regarding loading an excel file

647 views
Skip to first unread message

Parth

unread,
May 14, 2012, 1:55:12 AM5/14/12
to openpyxl-users
I am learning how to load an excel file using Openpyxl.

I wrote the following code:

from openpyxl import load_workbook

wb = load_workbook(filename = r'empty_book.xlsx')

sheet_ranges = wb.get_sheet_by_name(name = 'range names')

print sheet_ranges.cell('D18').value


This code works fine. However, if I replace the name of file by a file
created by me (InputFile.xlsx), following error is displayed:


Traceback (most recent call last):
File "C:/Python27/final.py", line 11, in <module>
wb = load_workbook(filename = r'InputFile.xlsx')
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line
86, in load_workbook
raise InvalidFileException(unicode(e))
InvalidFileException: File is not a zip file

Could anyone help me figure out the problem ??
P.S. this new file is in the same folder as empty_file.xlsx

Adam Morris

unread,
May 15, 2012, 8:11:24 AM5/15/12
to openpyx...@googlegroups.com
You can see if there is an error in the filename you're trying to read, by checking first if it exists:

from os.path import exists
if exists('empty_book.xlsx'):
   print 'Found the file'

If it doesn't exist, you might try setting the full path to the filename.

If it does exist, see if you can open the file in excel, and make sure that it's saved as an xlsx (and not the 97-2004 .xls format) - it's possible to save an xls file in excel with the wrong extensions if you manually set the extension and forget to change the filetype.  Excel doesn't tell you, it just quietly figures it out for itself.

If all else fails, make sure that it truly is a zip file - rename the file to InputFile.zip and see if your archive utility can open it.  If not... than it's not a valid xlsx file.

--Adam
Reply all
Reply to author
Forward
0 new messages