Tablib xlsx文件badZip文件问题

时间:2018-03-01 05:08:56

标签: python tablib

我在使用tablib库在Windows 8中打开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 detail and price list.xlsx'

1 个答案:

答案 0 :(得分:0)

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