1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 数字转化excel行列_在python/openpyx中将Excel行 列索引转换为字母数字单元格引用...

python 数字转化excel行列_在python/openpyx中将Excel行 列索引转换为字母数字单元格引用...

时间:2023-07-08 22:30:53

相关推荐

python 数字转化excel行列_在python/openpyx中将Excel行 列索引转换为字母数字单元格引用...

我想将行和列索引转换为一个Excel字母数字单元格引用,如“A1”。我正在使用python和openpyxl,我怀疑包中有一个实用程序可以做到这一点,但是经过一些搜索,我还没有找到任何东西。

我写了下面的代码,这很有效,但是如果openpyxl包可用的话,我宁愿使用它的一部分。def xlref(row,column):

"""

xlref - Simple conversion of row, column to an excel string format

>>> xlref(0,0)

'A1'

>>> xlref(0,26)

'AA1'

"""

def columns(column):

from string import uppercase

if column > 26**3:

raise Exception("xlref only supports columns < 26^3")

c2chars = [''] + list(uppercase)

c2,c1 = divmod(column,26)

c3,c2 = divmod(c2,26)

return "%s%s%s" % (c2chars[c3],c2chars[c2],uppercase[c1])

return "%s%d" % (columns(column),row+1)

有人知道更好的方法吗?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。