Last active
August 29, 2015 14:22
-
-
Save balzer82/7ecf318e207f124849ee to your computer and use it in GitHub Desktop.
If you want to convert between Excel Column (e.g. 'BF') and which number it is and back
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
import string | |
letter = string.ascii_uppercase | |
def num2excelcolumn(n): | |
''' | |
letter for the corresponding column | |
''' | |
if not isinstance(n, int): | |
raise Exception('Spalte kann nur ganze Zahl sein') | |
if n<27: | |
return letter[n-1] | |
elif n<703: | |
return letter[(n-1)/26-1] + letter[n%26-1] | |
elif n<17603: | |
return letter[(n-1)/(26*26)-1] + letter[(n-26*26-1)/26-1] + letter[n%26-1] | |
else: | |
raise Exception('Sorry, nur 3 Buchstaben!') | |
def excelcolumn2num(s): | |
''' | |
number for the corresponding column letters | |
''' | |
for i in range(17603): # just bruteforce it | |
if num2excelcolumn(i)==s: | |
return i | |
def e2i(s): | |
''' | |
Python index for the corresponding column letters | |
''' | |
for i in range(17603): # just bruteforce it | |
if num2excelcolumn(i)==s: | |
return i-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: