openpyxl.utils.exceptions.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.
时间: 2024-02-11 17:08:32 浏览: 25
这个错误提示说明openpyxl库不支持旧的.xls文件格式,需要使用xlrd库来读取该文件,或将其转换为较新的.xlsx文件格式。
如果你想使用openpyxl来处理Excel文件,可以将文件转换为.xlsx格式。可以使用Microsoft Excel或其他在线工具来进行转换。转换后,使用openpyxl库读取.xlsx文件即可。
如果你仍然想使用旧的.xls文件格式,可以使用xlrd库来读取它们。以下是一个示例代码:
```python
import xlrd
# 打开Excel文件
workbook = xlrd.open_workbook('example.xls')
# 选择工作表
sheet = workbook.sheet_by_index(0)
# 创建空列表,用于存储非空单元格的内容
non_empty_cells = []
# 遍历所有单元格,将非空单元格的内容添加到列表中
for row in range(sheet.nrows):
for col in range(sheet.ncols):
if sheet.cell(row, col).value != '':
non_empty_cells.append(sheet.cell(row, col).value)
# 输出非空单元格的内容列表
print(non_empty_cells)
```
其中,'example.xls'是旧的Excel文件的文件名,可以根据实际情况进行修改。这段代码会将Excel中所有的非空单元格的内容存储在列表non_empty_cells中,并打印输出。