Python的第三方库xlrd在Pypi上的地址:https://pypi.org/project/xlrd/
xlrd是一个用于读取xls格式的Excel文件的Python库,当前版本为2.0.1,xlrd在Poetry下的安装命令为:
poetry add xlrd
最简洁的xls格式的Excel文件的读取示例:
import xlrd
wb=xlrd.open_workbook("E:\\测试文件.xls")
# 2.获取sheet表格的数量
print(wb.nsheets)
# 3.获取所有sheet表格的名称
print(wb.sheet_names())
# 4.获取第一个sheet
sheet=wb.sheet_by_index(0)
# 5.获取指定sheet的名称、行数、列数
print("{0} {1} {2}".format(sheet.name, sheet.nrows, sheet.ncols))
# 6.获取指定单元格的值
print(sheet.cell(0,0).value)
# 7.获取指定单元格的数据类型:0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
print(sheet.cell(0,0).ctype)
此处需要注意的是如果一个单元格的值是通过公式计算出来的话,那么在使用xlrd来读取这个单元格的值时是读取不到的。
参考文档:
https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399