Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Bad magic number for file header" error while using openpyxl

I have made a python script which is meant to read an excel spreadsheet and return the value of cell A39. I'm using the openpyxl library.

Here is the part of the code that is giving an error:

cFile = openpyxl.load_workbook('contacts.xlsx', read_only= True)
sheet = cFile.get_sheet_by_name('cSheet')

print sheet['A39'].value

Instead of printing the value of cell A39, which in the spreadsheet is "38", I get the following error:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 971, in open
raise BadZipfile("Bad magic number for file header")
zipfile.BadZipfile: Bad magic number for file header

The spreadsheet 'contacts.xlsx' is not a zipped file. It is in the same folder as the python script. I made it with Excel 2011. Does anyone know why I'm getting this error or how I can fix it?

Thanks!

like image 472
Brianna Singer Avatar asked Jul 15 '17 17:07

Brianna Singer


1 Answers

When I had the same similar error, I also couldn't understand the issue. Also working with openpyxl library. My root problem was that some file was opened and save at the same time while using some kind of Multithreading. For a solution, I changed the name of the file according to every thread. you could also use some locks.

like image 124
raptor0102 Avatar answered Oct 22 '22 06:10

raptor0102