从列号中获取Excel样式的列名称

sau*_*ean 16 python excel python-2.7

这是在提供行和列ID时提供COLUMN名称的代码,但是当我给出类似值时row = 1 and col = 104,它应该返回CZ,但它返回D@

row = 1
col = 104
div = col
column_label = str()
while div:
    (div, mod) = divmod(div, 26)
    column_label = chr(mod + 64) + column_label

print column_label
Run Code Online (Sandbox Code Playgroud)

我在做什么有什么问题?

(此代码参考EXCEL列,其中我提供了行,列ID值并期望ALPHABETIC值相同.)

mar*_*eau 20

编辑:我觉得我必须承认,正如其他一些人 - 他从未给我留下评论 - 指出的那样,我的答案的先前版本(你接受了)有一个错误,导致它无法正确处理大于的列数702(对应于Excel专栏'ZZ').因此,为了正确性,这已在下面的代码中得到修复,现在它包含一个循环,就像许多其他答案一样.

很可能你从来没有使用过去有足够大列数的先前版本来遇到这个问题.FWIW,当前版本的ExcelMS规格说它支持最多16,384列的工作表(Excel专栏'XFD').

LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def excel_style(row, col):
    """ Convert given row and column number to an Excel-style cell name. """
    result = []
    while col:
        col, rem = divmod(col-1, 26)
        result[:0] = LETTERS[rem]
    return ''.join(result) + str(row)

if __name__ == '__main__':
    addresses = [(1,  1), (1, 26),
                 (1, 27), (1, 52),
                 (1, 53), (1, 78),
                 (1, 79), (1, 104),
                 (1, 18253), (1, 18278),
                 (1, 702),  # -> 'ZZ1'
                 (1, 703),  # -> 'AAA1'
                 (1, 16384), # -> 'XFD1'
                 (1, 35277039)]

    print('({:3}, {:>10}) --> {}'.format('row', 'col', 'Excel'))
    print('==========================')
    for row, col in addresses:
        print('({:3}, {:10,}) --> {!r}'.format(row, col, excel_style(row, col)))
Run Code Online (Sandbox Code Playgroud)

输出:

(row,       col) --> Excel
========================
(  1,         1) --> 'A1'
(  1,        26) --> 'Z1'
(  1,        27) --> 'AA1'
(  1,        52) --> 'AZ1'
(  1,        53) --> 'BA1'
(  1,        78) --> 'BZ1'
(  1,        79) --> 'CA1'
(  1,       104) --> 'CZ1'
(  1,     18253) --> 'ZZA1'
(  1,     18278) --> 'ZZZ1'
(  1,       702) --> 'ZZ1'
(  1,       703) --> 'AAA1'
(  1,     16384) --> 'XFD1'
(  1,  35277039) --> 'BYEBYE1'
Run Code Online (Sandbox Code Playgroud)


Nod*_*ody 13

您有几个索引问题:

因此,要解决您的问题,您需要使所有索引匹配:

def colToExcel(col): # col is 1 based
    excelCol = str()
    div = col 
    while div:
        (div, mod) = divmod(div-1, 26) # will return (x, 0 .. 25)
        excelCol = chr(mod + 65) + excelCol

    return excelCol

print colToExcel(1) # => A
print colToExcel(26) # => Z
print colToExcel(27) # => AA
print colToExcel(104) # => CZ
print colToExcel(26**3+26**2+26) # => ZZZ
Run Code Online (Sandbox Code Playgroud)


Ken*_*n T 8

我喜欢maritineau的答案,因为它的代码看起来简单易懂.但它无法处理大于26**2 + 26的列号.所以我修改了它的一部分.

def excel_col(col):
    """Covert 1-relative column number to excel-style column label."""
    quot, rem = divmod(col-1,26)
    return excel_col(quot) + chr(rem+ord('A')) if col!=0 else ''



if __name__=='__main__':
    for i in [1, 26, 27, 26**3+26**2+26]:
        print 'excel_col({0}) -> {1}'.format(i, excel_col(i))
Run Code Online (Sandbox Code Playgroud)

结果

excel_col(1) -> A
excel_col(26) -> Z
excel_col(27) -> AA
excel_col(18278) -> ZZZ
Run Code Online (Sandbox Code Playgroud)


use*_*292 5

def ColNum2ColName(n):
   convertString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   base = 26
   i = n - 1

   if i < base:
      return convertString[i]
   else:
      return ColNum2ColName(i//base) + convertString[i%base]
Run Code Online (Sandbox Code Playgroud)

编辑:对,对 zondo。

我只是A, B, .. AA, AB, ...作为一个带数字的数字基础接近A-Z

A = 1
B = 2
 .
 .
X = 24
Y = 25
Z = 26
 .
 .
 .
Run Code Online (Sandbox Code Playgroud)

这是一种没有任何 while 循环等的简单方法,适用于任何数字> 0