1

嗨,我只是拿起 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:
4

1 回答 1

1

你是在读还是写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/

于 2012-07-30T04:39:47.670 回答