问题
I have been working on a script on Windows and now when changing the platform to Linux/Ubuntu I am having some issues. In the example below I am just trying to print the sheetnames but receicing some errors which I have never seen before. May this be a issue with Xlrd ? Because I am able to open the Excel file in Libreoffice.
Code:
#!/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)
Error:
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:
If you need data from a remote service, it's a hack, but commenting out line 418 (raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s])))
in compdoc.py seems to work okay.
Also look into this documentation source
来源:https://stackoverflow.com/questions/34550624/python-xlrd-read-excel-file-error