Python Xlrd读取excel文件错误

时间:2015-12-31 19:00:07

标签: python excel xlrd

我一直在Windows上编写脚本,现在将平台更改为Linux / Ubuntu时,我遇到了一些问题。在下面的示例中,我只是尝试打印工作表名称,但收到了一些我以前从未见过的错误。这可能是Xlrd的问题吗?因为我能够在Libreoffice中打开Excel文件。

代码:

#!/usr/bin/python
import xlrd

xl_workbook = xlrd.open_workbook('/home/ubuntu/Downloads/usage01.12.2015_31.12.2015.xls')

sheet_names = xl_workbook.sheet_names()
print('Sheet Names', sheet_names)

错误:

    0  5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
   20  4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
   40= 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
   60  4 4 3 2
Traceback (most recent call last):
  File "./forbruk.py", line 4, in <module>
    xl_workbook = xlrd.open_workbook('/home/ubuntu/Downloads/usage01.12.2015_31.12.2015.xls')
  File "/usr/local/lib/python2.7/dist-packages/xlrd-0.9.4-py2.7.egg/xlrd/__init__.py", line 441, in open_workbook
    ragged_rows=ragged_rows,
  File "/usr/local/lib/python2.7/dist-packages/xlrd-0.9.4-py2.7.egg/xlrd/book.py", line 87, in open_workbook_xls
    ragged_rows=ragged_rows,
  File "/usr/local/lib/python2.7/dist-packages/xlrd-0.9.4-py2.7.egg/xlrd/book.py", line 592, in biff2_8_load
    cd.locate_named_stream(UNICODE_LITERAL(qname))
  File "/usr/local/lib/python2.7/dist-packages/xlrd-0.9.4-py2.7.egg/xlrd/compdoc.py", line 390, in locate_named_stream
    d.tot_size, qname, d.DID+6)
  File "/usr/local/lib/python2.7/dist-packages/xlrd-0.9.4-py2.7.egg/xlrd/compdoc.py", line 418, in _locate_stream
    raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s]))
xlrd.compdoc.CompDocError: Workbook corruption: seen[2] == 4

1 个答案:

答案 0 :(得分:1)

如果你需要来自远程服务的数据,这是一个黑客攻击,但在compdoc.py中注释掉第418行(raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s])))似乎工作正常。

另请参阅此文档source