0

I am using openpyxl to work with excel sheets in python. I am not able to open the workbook by entering the path to the file or just the name entered as a variable. How can I solve this?

filename = 'file.xlsx'
wb = openpyxl.load_workbook(filename)

I get the following error:

wb = openpyxl.load_workbook(filename)
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\zipfile.py", line 1269, in __init__     
    self._RealGetContents()
  File "C:\Users\d\AppData\Local\Programs\Python\Python38\lib\zipfile.py", line 1336, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

However, if I put the file name directly in the function it loads it, and I do not get any error.

wb = openpyxl.load_workbook('file.xlsx')
Darío
  • 23
  • 5

0 Answers0