Skip to content

Instantly share code, notes, and snippets.

@rebornix
Created August 1, 2012 15:32
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 rebornix/3227889 to your computer and use it in GitHub Desktop.
Save rebornix/3227889 to your computer and use it in GitHub Desktop.
Convert between Column No & Column Name in Excel.
import sys
import string
def NameToNo(name):
result = 0
for ch in name.upper().strip("\n"):
result = result * 26 + ord(ch) - ord('A') + 1
return result
def NoToName(no):
columnNum = string.atoi(no)
result = ""
Exp = 26
while ( columnNum > Exp ):
Exp = Exp * 26
while ( columnNum != 0 ):
Exp = Exp / 26
count = columnNum / Exp
result = result + chr(count - 1 + ord('a')).upper()
columnNum = columnNum - count * Exp
return result
if __name__ == "__main__":
while True:
print "Choose 0) Name to No. 1) No to Name. 2) Exit"
type = sys.stdin.readline()
if( string.atoi(type) == 0 ):
print "Enter Name: "
print NameToNo(sys.stdin.readline())
elif ( string.atoi(type) == 1):
print "Enter Column No:"
print NoToName(sys.stdin.readline())
else:
break
print "Byebye Love"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment