我在Ubuntu上使用Python 2.7.11。
我尝试使用xlrd
包在Python中打开Excel文件(.xlsx)。但是,当我尝试使用程序包中的open_workbook()
函数打开我的Excel文件时出现以下错误:
Traceback (most recent call last):
File "TileInserter.py", line 15, in <module>
book = open_workbook(sheetPath, on_demand=True)
File "/usr/local/lib/python2.7/site-packages/xlrd/__init__.py", line 422, in open_workbook
ragged_rows=ragged_rows,
File "/usr/local/lib/python2.7/site-packages/xlrd/xlsx.py", line 761, in open_workbook_2007_xml
zflo = zf.open(component_names['xl/_rels/workbook.xml.rels'])
File "/usr/local/lib/python2.7/zipfile.py", line 1010, in open
close_fileobj=should_close)
File "/usr/local/lib/python2.7/zipfile.py", line 526, in __init__
self._decompressor = zlib.decompressobj(-15)
AttributeError: 'NoneType' object has no attribute 'decompressobj'
我尝试了解此错误的原因并发现如果未安装zlib
库,可能会发生这种情况。但是,当我使用PHP phpinfo()
函数进行检查时,它显示已安装zlib
。这也是最新版本(版本1.2.8)。
所以我现在有点卡住了。有谁知道如何解决这个问题?
编辑:TileInserter.py
中的实际代码如下所示(TileInserter.py
和TileList.xlsx
位于同一目录中):
from xlrd import open_workbook
sheetPath = "TileList.xlsx"
#some more variables
#Open Excel file
book = open_workbook(sheetPath, on_demand=True)
for name in book.sheet_names():
if name.endswith('1'):
sheet = book.sheet_by_name(name)