Pycharm 读取Excel文件报错

import xlrd

def read_excel(file_name,index):
excel = xlrd.open_workbook(file_name)
sheet = excel.sheets()[index]
return sheet

if name == 'main':
table = read_excel('2.xlsx ', 0)
for i in range(1,table.nrows):
rows = table.row_values(i)
print(rows)

img

原因:xlrd1.2.0之后的版本不支持xlsx格式,支持xls格式

办法一:

卸载新版本 pip uninstall xlrd

安装老版本:pip install xlrd=1.2.0 (或者更早版本)

方法二:

将xlrd用到的excel版本格式修改为xls(保险起见,另存为xls格式)

建议:个人更推荐使用第二种方法