当前位置:   article > 正文

python应用-高效使用xlrd库读取excel表内容_python xlrd

python xlrd

学习目录

了解下电脑中的excel表格文件格式 

安装xlrd库

xlrd库读取表格内容

1 先准备一个表格‘表格.xls’,表格中包含两个sheet页

2 导入xlrd库

3 用一个图展示下xlrd常用的函数

4 分别展示下表格中按行/按列/按单元格获取的内容

5 拓展内容

excel表格是大家经常用到的文件格式,各行各业都会跟它打交道。本次我们介绍经常用到的两个经典库,xlrd和xlwt,xlrd用于读取excel表格内容,xlwt用于写入excel表格内容。

了解下电脑中的excel表格文件格式

微软或者金山的excel表格编辑保存时一般要选择文件后缀,有xls和xlsx两类。

xls和xlsx后缀文件的主要区别:

  • 文件格式:xls是二进制格式,而xlsx是基于XML的压缩方式。
  • 版本:xls是Excel 2003及以前版本生成的文件格式,而xlsx是Excel 2007及以后版本生成的文件格式。
  • 兼容性:xlsx格式向下兼容,而xls格式不支持向后兼容。

安装xlrd库

pip install xlrd -i https://mirrors.aliyun.com/pypi/simple/

xlrd库默认安装最新的库,新版本的xlrd只支持.xls文件,如果需要读取.xlsx文件时需要安装旧版本(比如指定版本xlrd==1.2.0)。

xlrd库读取表格内容

1 先准备一个表格‘表格.xls’,表格中包含两个sheet页。

2 导入xlrd库

执行import xlrd导入该库

3 用一个图展示下xlrd常用的函数

4 分别展示下表格中按行/按列/按单元格获取的内容

  1. 按行获取内容
  • 获取总行数
  1. #打开表格
  2. data = xlrd.open_workbook('表格.xls')
  3. #获取sheet1的内容
  4. sheet1 = data.sheets()[0]
  5. #行数
  6. sheet1_nrows = sheet1.nrows
  7. print(f'sheet1 行数:{sheet1_nrows}')
  8. #结果
  9. sheet1 行数:4
  • row()函数获取的内容
  1. for i in range(sheet1_nrows):
  2. sheet1_row_content = sheet1.row(i)
  3. print(f'sheet1 第{i+1}行内容:{sheet1_row_content}')
  4. #结果:
  5. sheet11行内容:[text:'user', text:'age']
  6. sheet12行内容:[text:'lili', number:21.0]
  7. sheet13行内容:[text:'zhangsan', number:13.0]
  8. sheet14行内容:[text:'lisi', number:35.0]
  • row_values()函数获取的内容
  1. for i in range(sheet1_nrows):
  2. sheet1_row_content = sheet1.row_values(i)
  3. print(f'sheet1 第{i+1}行内容:{sheet1_row_content}')
  4. #结果
  5. sheet11行内容:['user', 'age']
  6. sheet12行内容:['lili', 21.0]
  7. sheet13行内容:['zhangsan', 13.0]
  8. sheet14行内容:['lisi', 35.0]
  • get_rows()函数返回一个生成器,也是可迭代对象
  1. for i in sheet1.get_rows():
  2. print(f'sheet1 中的行内容:{i}')
  3. #结果:
  4. sheet1 中的行内容:[text:'user', text:'age']
  5. sheet1 中的行内容:[text:'lili', number:21.0]
  6. sheet1 中的行内容:[text:'zhangsan', number:13.0]
  7. sheet1 中的行内容:[text:'lisi', number:35.0]

2 按列获取内容

  • 获取总列数
  1. sheet1_ncols = sheet1.ncols
  2. print(f'sheet1 列数:{sheet1_ncols}')
  • col()函数获取的内容
  1. for i in range(sheet1_ncols):
  2. sheet1_col_content = sheet1.col(i)
  3. print(f'sheet1 第{i+1}列内容:{sheet1_col_content}')
  4. #结果:
  5. sheet11列内容:[text:'user', text:'lili', text:'zhangsan', text:'lisi']
  6. sheet12列内容:[text:'age', number:21.0, number:13.0, number:35.0]
  • col_values()函数获取的内容
  1. for i in range(sheet1_ncols):
  2. sheet1_col_content = sheet1.col_values(i)
  3. print(f'sheet1 第{i+1}列内容:{sheet1_col_content}')
  4. #结果
  5. sheet11列内容:['user', 'lili', 'zhangsan', 'lisi']
  6. sheet12列内容:['age', 21.0, 13.0, 35.0]

3 按单元格获取内容

  1. print(f'sheet1 第1行 第2列内容:{sheet1.cell(0, 1).value}')
  2. print(f'sheet1 第2行 第2列内容:{sheet1.cell_value(1, 1)}')
  3. print(f'sheet1 第1行 第1列内容:{sheet1.row(0)[0].value}')
  4. #结果:
  5. sheet11行 第2列内容:age
  6. sheet12行 第2列内容:21.0
  7. sheet11行 第1列内容:user

5 拓展内容

1)表格中数据类型介绍

0 - empty, 1 - text, 2 - number, 3 - date, 4 - boolean, 5 - error

表格sheet3中有如下内容:

获取数据类型如下:

  1. sheet3 = data.sheets()[2]
  2. row_type = sheet3.row_types(0)
  3. print(row_type)
  4. col_type = sheet3.col_types(1)
  5. print(col_type)
  6. cell_type = sheet3.cell_type(0,3)
  7. print(cell_type)
  8. #结果:
  9. array('B', [0, 1, 2, 3, 4, 1])
  10. [1]
  11. 3

2 获取行内容时发现填写的2023年10月8日返回的是数字

  1. cell_content = sheet3.cell(0,3).value
  2. print(cell_content)
  3. #结果:
  4. 45207.0

原来excel表中的日期会被python根据一个时间基准计算出相差的的天数(1900-01-01和1904-01-01基准)

解决方法:使用
xlrd.xldate.xldate_as_datetime函数处理得到的数字,它会返回datetime对象,用strftime把它转化成指定格式的字符串。

print(xlrd.xldate.xldate_as_datetime(cell_content,0).strftime("%Y/%m/%d"))
结果:2023/10/08

----感谢读者的阅读和学习,谢谢大家。

共勉: 东汉·班固《汉书·枚乘传》:“泰山之管穿石,单极之绠断干。水非石之钻,索非木之锯,渐靡使之然也。”

-----指水滴不断地滴,可以滴穿石头;

-----比喻坚持不懈,集细微的力量也能成就难能的功劳。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/910745
推荐阅读
相关标签
  

闽ICP备14008679号