我在 Windows 8 中使用 tablib 库打开 xlsx 扩展文件时遇到错误。
Python 版本 - 2.7.14
错误如下:
python suit_simple_sheet_product.py
Traceback (most recent call last):
File "suit_simple_sheet_product.py", line 19, in <module>
data = tablib.Dataset().load(open(BASE_PATH).read())
File "C:\Python27\lib\site-packages\tablib\core.py", line 446, in load
format = detect_format(in_stream)
File "C:\Python27\lib\site-packages\tablib\core.py", line 1157, in detect_format
if fmt.detect(stream):
File "C:\Python27\lib\site-packages\tablib\formats\_xls.py", line 25, in detect
xlrd.open_workbook(file_contents=stream)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 120, in open_workbook
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
File "C:\Python27\lib\zipfile.py", line 770, in __init__
self._RealGetContents()
File "C:\Python27\lib\zipfile.py", line 811, in _RealGetContents
raise BadZipfile, "File is not a zip file"
zipfile.BadZipfile: File is not a zip file
路径位置如下= BASE_PATH = 'C:\Users\anju\Downloads\automate\catalog-5090 Fabric 详细信息和价目表.xlsx'
最佳答案
Excel .xlsx
文件实际上是 zip 文件。为了使解压缩正常工作,必须以二进制模式打开文件,因此您需要使用以下命令打开文件:
import tablib
BASE_PATH = r'c:\my folder\my_test.xlsx'
data = tablib.Dataset().load(open(BASE_PATH, 'rb').read())
print data
在字符串前添加 r
以阻止 Python 尝试解释路径中的反斜杠字符。
关于python - Tablib xlsx 文件 badZip 文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49042976/