1

在 Windows 8 中使用 tablib 库打开 xlsx 扩展文件时出现错误。

蟒蛇版本 - 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 detail and price list.xlsx'

4

1 回答 1

1

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 尝试解释路径中的反斜杠字符。

于 2018-03-01T08:28:38.727 回答