Python错误集锦:openpyxl打开xls格式excel文件提示InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.

原文链接:http://www.juzicode.com/python-error-openpyxl-does-not-support-the-old-xls-file-format

错误提示:

openpyxl打开xls格式excel文件提示InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.

#juzicode.com/VX公众号:juzicode 
from openpyxl import load_workbook 
wb = load_workbook('profile.xls') 
ws = wb.active 
vals = []
for row in ws.iter_rows(values_only=True): # 迭代读出
    vals.append(row)
    print(row) 
==========运行结果:
File D:\Python\Python310\lib\site-packages\openpyxl\reader\excel.py:123, in ExcelReader.__init__(self, fn, read_only, keep_vba, data_only, keep_links, rich_text)
    121 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
    122              data_only=False, keep_links=True, rich_text=False):
--> 123     self.archive = _validate_archive(fn)
    124     self.valid_files = self.archive.namelist()
    125     self.read_only = read_only

File D:\Python\Python310\lib\site-packages\openpyxl\reader\excel.py:93, in _validate_archive(filename)
     87         else:
     88             msg = ('openpyxl does not support %s file format, '
     89                    'please check you can open '
     90                    'it with Excel first. '
     91                    'Supported formats are: %s') % (file_format,
     92                                                    ','.join(SUPPORTED_FORMATS))
---> 93         raise InvalidFileException(msg)
     95 archive = ZipFile(filename, 'r')
     96 return archive

InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.

错误原因:

1、openpyxl模块不支持xls格式的excel表格,

解决方法:

1、需要将xls转换为xlsx格式再打开文件,比如打开文件后另存为xlsx格式。

#juzicode.com/VX公众号:juzicode 
from openpyxl import load_workbook 
wb = load_workbook('profile.xlsx')  # 将文件另存为xlsx后再打开
ws = wb.active 
vals = []
for row in ws.iter_rows(values_only=True): # 迭代读出
    vals.append(row)
    print(row) 

扩展内容:


如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注