Created
December 12, 2017 12:52
-
-
Save dinya/515a68367ab6b2921560a3babaf0a682 to your computer and use it in GitHub Desktop.
Cell names to (row, col) tuple ready for using with xlrd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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