gpt4 book ai didi

python xlrd - 从工作表中检索列索引

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:51 26 4
gpt4 key购买 nike

嗨,我刚刚拿起 xlrd。关于访问工作表和单元格属性,我指的是Xlrd Column

那里的代码显示。

for crange in thesheet.col_label_ranges:
rlo, rhi, clo, chi = crange
for rx in xrange(rlo, rhi):
for cx in xrange(clo, chi):
print "Column label at (rowx=%d, colx=%d) is %r" \
(rx, cx, thesheet.cell_value(rx, cx))

所以我想我只是测试从“数据”表中打印单元格 A1,所以我开始复制上面的示例。

当它完成时,但它在 col_label_ranges 处出错:

inBook = xlrd.open_workbook('T:/AzReporting/DraftUtilization.xls')
outBook = xlutils.copy.copy(inBook)
for crange in outBook.col_label_ranges:
rlo, rhi, clo, chi = crange
for rx in xrange(rlo, rhi):
for cx in xrange(clo, chi):
print "Column label at (rowx=%d, colx=%d) is %r" \
(rx, cx, outBook.cell_value(0, 0))

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'Workbook' object has no attribute 'col_label_ranges'

此外,如果我将 col_label_names 更改为工作表名称,它也会出错。我一定是在这个例子中遗漏了一些东西。也许有更好的教程可以遵循?

for crange in outBook.Data:

最佳答案

您正在读取或写入 Excel 文件吗? xlrd用于读取excel文件,xlwt用于写入

我相信您错过了之间的中间步骤

           inBook = xlrd.open_workbook('T:/AzReporting/DraftUtilization.xls')

           for crange in outBook.col_label_ranges:

您必须指定 Excel 文件的工作表

甚至这个例子也被标记为“thesheet”

我想改变

           for crange in outBook.col_label_ranges:

           sh=inBook.sheet_by_index(0)
for crange in sh.col_label_ranges:

http://www.numbergrinder.com/2008/10/pulling-data-from-excel-using-python-xlrd/

关于python xlrd - 从工作表中检索列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com