Tablib xlsx file badZip file issue

518 Views Asked by At

I am getting error on opening xlsx extension file in windows 8 using tablib library.

python version - 2.7.14

error is as follows:

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

path location is as follows = BASE_PATH = 'C:\Users\anju\Downloads\automate\catalog-5090 fabric detail and price list.xlsx'

1

There are 1 best solutions below

0
On

Excel .xlsx files are actually zip files. In order for the unzip to work correctly, the file must be opened in binary mode, as such your need to open the file using:

import tablib

BASE_PATH = r'c:\my folder\my_test.xlsx'
data = tablib.Dataset().load(open(BASE_PATH, 'rb').read())

print data

Add r before your string to stop Python from trying to interpret the backslash characters in your path.