openpyxl库
pip install openpyxl
读取Excel
import openpyxl file = "D:\\桌面\\2022年暑假留校学生情况统计表_ghg.xlsx" # file = r'D:\PYTHON\openpyxl_read_write\\数据管理系统.xlsx' workbook1=openpyxl.load_workbook(file) #选定目标sheet worksheet1 = workbook1.active col_A = [] col_B= [] for cell in worksheet1['A']: # print(cell.value) col_A.append(cell.value) print(col_A) for cell in worksheet1['C']: # print(cell.value) col_B.append(cell.value) print(col_B)
报错
xlrd.biffh.XLRDError: Excel xlsx file; not supported
原因是最近xlrd更新到了2.0.1版本,只支持.xls文件。所以pandas.read_excel(‘xxx.xlsx’)会报错。
可以安装旧版xlrd,在cmd中运行:
pip uninstall xlrdpip install xlrd==1.2.0
也可以用openpyxl代替xlrd打开.xlsx文件:
df=pandas.read_excel(‘data.xlsx’,engine=‘openpyxl’)