I\'m looking for the opposite to this Q&A: Convert an excel or spreadsheet column letter to its number in Pythonic fashion.
or this one but in python How to conv
The openpyxl library includes the conversion function (amongst others) which you are looking for, get_column_letter
:
>>> from openpyxl.utils.cell import get_column_letter
>>> get_column_letter(1)
'A'
>>> get_column_letter(10)
'J'
>>> get_column_letter(3423)
'EAQ'
My recipe for this was inspired by another answer on arbitrary base conversion (https://stackoverflow.com/a/24763277/3163607)
import string
def n2a(n,b=string.ascii_uppercase):
d, m = divmod(n,len(b))
return n2a(d-1,b)+b[m] if d else b[m]
Example:
for i in range(23,30):
print (i,n2a(i))
outputs
23 X
24 Y
25 Z
26 AA
27 AB
28 AC
29 AD
Here is a recursive solution:
def column_num_to_string(n):
n, rem = divmod(n - 1, 26)
char = chr(65 + rem)
if n:
return column_num_to_string(n) + char
else:
return char
column_num_to_string(28)
#output: 'AB'
The inverse can also be defined recursively, in a similar way:
def column_string_to_num(s):
n = ord(s[-1]) - 64
if s[:-1]:
return 26 * (column_string_to_num(s[:-1])) + n
else:
return n
column_string_to_num("AB")
#output: 28
Edited after some tough love from Meta
The procedure for this involves dividing the number by 26 until you've reached a number less than 26, taking the remainder each time and adding 65, since 65 is where 'A' is in the ASCII table. Read up on ASCII if that doesn't make sense to you.
Note that like the originally linked question, this is 1-based rather than zero-based, so A -> 1
, B -> 2
.
def num_to_col_letters(num):
letters = ''
while num:
mod = (num - 1) % 26
letters += chr(mod + 65)
num = (num - 1) // 26
return ''.join(reversed(letters))
Example output:
for i in range(1, 53):
print i, num_to_col_letters(i)
1 A
2 B
3 C
4 D
...
25 Y
26 Z
27 AA
28 AB
29 AC
...
47 AU
48 AV
49 AW
50 AX
51 AY
52 AZ
The xlsxwriter library includes a conversion function, xlsxwriter.utility.xl_col_to_name(index)
and is on github
here is a working example:
>>> import xlsxwriter
>>> xlsxwriter.utility.xl_col_to_name(10)
'K'
>>> xlsxwriter.utility.xl_col_to_name(1)
'B'
>>> xlsxwriter.utility.xl_col_to_name(0)
'A'
Notice that it's using zero-indexing.
Recursive Implementation
import string
def spreadsheet_column_encoding_reverse_recursive(x):
def converter(x):
return (
""
if x == 0
else converter((x - 1) // 26) + string.ascii_uppercase[(x - 1) % 26]
)
return converter(x)
Iterative Implementations
Version 1: uses chr
, ord
def spreadsheet_column_encoding_reverse_iterative(x):
s = list()
while x:
x -= 1
s.append(chr(ord("A") + x % 26))
x //= 26
return "".join(reversed(s))
Version 2: Uses string.ascii_uppercase
import string
def spreadsheet_column_encoding_reverse_iterative(x):
s = list()
while x:
x -= 1
s.append(string.ascii_uppercase[x % 26])
x //= 26
return "".join(reversed(s))
Version 3: Uses divmod
, chr
, ord
def spreadsheet_column_encoding_reverse_iterative(x):
s = list()
while x:
x, remainder = divmod(x - 1, 26)
s.append(chr(ord("A") + remainder))
return "".join(reversed(s))