Skip to content

Instantly share code, notes, and snippets.

@dinya
Created December 12, 2017 12:52
Show Gist options
  • Select an option

  • Save dinya/515a68367ab6b2921560a3babaf0a682 to your computer and use it in GitHub Desktop.

Select an option

Save dinya/515a68367ab6b2921560a3babaf0a682 to your computer and use it in GitHub Desktop.
Cell names to (row, col) tuple ready for using with xlrd
def colNameToNum(name):
"""
http://cwestblog.com/2013/09/13/python-convert-excel-column-name-to-number/
"""
pow = 1
colNum = 0
for letter in name[::-1]:
colNum += (int(letter, 36) -9) * pow
pow *= 26
return colNum
import re
r = re.compile("([a-zA-Z]+)([0-9]+)")
def get_index_from_name(cell_name):
"""Returns (row, col) tuple from cell name:
"A1" -> (0, 0)
"""
m = r.match(cell_name.lower())
col = colNameToNum(m.group(1)) - 1
row = int(m.group(2)) - 1
return row, col
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment