Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python xlrd unsupported format, or corrupt file.

Tags:

python

excel

xlrd

My code:

import xlrd wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls") sh = wb.sheet_by_index(0) print sh.cell(0,0).value 

The error:

Traceback (most recent call last): File "Z:\Wilson\tradedStockStatus.py", line 18, in <module> wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls") File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 429, in open_workbook biff_version = bk.getbof(XL_WORKBOOK_GLOBALS) File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1545, in getbof bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8]) File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1539, in bof_error raise XLRDError('Unsupported format, or corrupt file: ' + msg) xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '<table r'" 

The file doesn't seem to be corrupted or of a different format. Anything to help find the source of the issue would be great.

like image 802
wDroter Avatar asked Mar 08 '12 18:03

wDroter


People also ask

What is BOF error?

The error message relates to the BOF (Beginning of File) record of an XLS file. However, the example shows that you are trying to read an XLSX file. There are 2 possible reasons for this: Your version of xlrd is old and doesn't support reading xlsx files.


2 Answers

Try to open it as an HTML with pandas:

import pandas as pd data = pd.read_html('filename.xls') 

Or try any other html python parser.

That's not a proper excel file, but an html readable with excel.

like image 115
foebu Avatar answered Sep 19 '22 12:09

foebu


You say:

The file doesn't seem to be corrupted or of a different format.

However as the error message says, the first 8 bytes of the file are '<table r' ... that is definitely not Excel .xls format. Open it with a text editor (e.g. Notepad) that won't take any notice of the (incorrect) .xls extension and see for yourself.

like image 28
John Machin Avatar answered Sep 19 '22 12:09

John Machin