openpyxl load_workbook加载excel报错zipfile.BadZipFile: File is not a zip file

问题

使用openpyxl load_workbook函数加载已存在的excel文件,出现一些报错:

Traceback (most recent call last):
  File "test1.py", line 28, in <module>
    split_excel("./test2.xlsx")
  File "test1.py", line 14, in split_excel
    wb = load_workbook(file_path)
  File "D:\softwares\Env\Python3\lib\site-packages\openpyxl\reader\excel.py", line 344, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "D:\softwares\Env\Python3\lib\site-packages\openpyxl\reader\excel.py", line 123, in __init__
    self.archive = _validate_archive(fn)
  File "D:\softwares\Env\Python3\lib\site-packages\openpyxl\reader\excel.py", line 95, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "D:\softwares\Env\Python3\lib\zipfile.py", line 1269, in __init__
    self._RealGetContents()
  File "D:\softwares\Env\Python3\lib\zipfile.py", line 1336, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

解决

上面那个excel文件是我通过vs code创建的,试了几次,只要是通过vs code创建的excel文件,通过load_workbook加载时都会报以上的错误。而当我通过excel表相关软件创建excel文件,则是可以加载成功。

猜你喜欢

转载自blog.csdn.net/weixin_42492572/article/details/132640255