Skip to content

Instantly share code, notes, and snippets.

@joferkington
Created October 10, 2012 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joferkington/3862933 to your computer and use it in GitHub Desktop.
Save joferkington/3862933 to your computer and use it in GitHub Desktop.
excel-style label conversion
"""MIT license"""
import xlrd
import re
def col2index(name):
name = name.upper()
col = -1
for i, letter in enumerate(name[::-1]):
col += (ord(letter) - ord('A') + 1) * 26**i
return col
def row2index(name):
return int(name) - 1
def cell2index(name):
colname, rowname, _ = re.split(r'(\d+)', name)
return row2index(rowname), col2index(colname)
for i in range(1000):
assert i == col2index(xlrd.colname(i))
print cell2index('AB1243')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment