Created
August 1, 2012 15:32
-
-
Save rebornix/3227889 to your computer and use it in GitHub Desktop.
Convert between Column No & Column Name in Excel.
This file contains 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 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