使用 Python 的第三方库 xlrd 读取 Excel 文件

使用 Python 的第三方库 xlrd 读取 Excel 文件

目录

  • [使用 Python 的第三方库 xlrd 读取 Excel 文件](#使用 Python 的第三方库 xlrd 读取 Excel 文件)

一、安装 xlrd 库

xlrd是读Excel的库,xlrd是第三方库,因此需要先安装才能使用。在Windows命令行窗口输入如下命令安装 xlrd库:

shell 复制代码
C:\Users\Administrator>pip install xlrd
Collecting xlrd
  Downloading xlrd-2.0.2-py2.py3-none-any.whl.metadata (3.5 kB)
Downloading xlrd-2.0.2-py2.py3-none-any.whl (96 kB)
Installing collected packages: xlrd
Successfully installed xlrd-2.0.2

[notice] A new release of pip is available: 25.2 -> 25.3
[notice] To update, run: python.exe -m pip install --upgrade pip

pip包进行更新:

shell 复制代码
C:\Users\Administrator>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\users\administrator\appdata\local\programs\python\python313\lib\site-packages (25.2)
Collecting pip
  Downloading pip-25.3-py3-none-any.whl.metadata (4.7 kB)
Downloading pip-25.3-py3-none-any.whl (1.8 MB)
   ---------------------------------------- 1.8/1.8 MB 4.6 MB/s  0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 25.2
    Uninstalling pip-25.2:
      Successfully uninstalled pip-25.2
Successfully installed pip-25.3

由于所安装的第三方库默认是在国外的服务器上,所以可能会出现安装速度比较慢的情况。此时可以尝试使用国内镜像源的方式进行安装。国内镜像源网址如下:

清华大学:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http:/mirrors.aliyun.com/pypi/simple

镜像源使用方式:在使用pip时添加参数-i https://pypi.tuna.tsinghua.edu.cn/simple

例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd

二、使用 xlrd 库读取 Excel 文件

1、打开 Excel 工作表

理解工作簿、工作表、单元格区域、单元格等概念。

python 复制代码
import xlrd
# 使用 open_workbook 加载磁盘中的Excel文件
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")

# 执行以上代码时程序报如下错误:
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python313/aa.py", line 2, in <module>
    excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\xlrd\__init__.py", line 170, in open_workbook
    raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported

在 CSDN 搜索,给出的原因如下:通常是由于当前 Python 中的 xlrd 版本过高,高版本下删除了对应的 .xlsx 读取方法,或者 xlrd 更新到了 2.0.1 版本,只支持 .xls 文件。

解决方法为:重装指定版本的 xlrd。在命令行中先卸载现有的 xlrd,再安装指定版本的 xlrd。

shell 复制代码
C:\Users\Administrator>pip uninstall xlrd
Found existing installation: xlrd 2.0.2
Uninstalling xlrd-2.0.2:
  Would remove:
    c:\users\administrator\appdata\local\programs\python\python313\lib\site-packages\xlrd-2.0.2.dist-info\*
    c:\users\administrator\appdata\local\programs\python\python313\lib\site-packages\xlrd\*
    c:\users\administrator\appdata\local\programs\python\python313\scripts\runxlrd.py
Proceed (Y/n)? y
  Successfully uninstalled xlrd-2.0.2

C:\Users\Administrator>pip install xlrd==1.2.0
Collecting xlrd==1.2.0
  Downloading xlrd-1.2.0-py2.py3-none-any.whl.metadata (1.3 kB)
Downloading xlrd-1.2.0-py2.py3-none-any.whl (103 kB)
Installing collected packages: xlrd
Successfully installed xlrd-1.2.0

重新编写代码,加载 Excel 文件,获取工作表中的数据。

加载 Excel 文件,查看对象类型。代码如下:

python 复制代码
import xlrd
# 使用 open_workbook 加载磁盘中的Excel文件
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
print(type(excelbook))
print(excelbook)

程序运行结果为:
<class 'xlrd.book.Book'>
<xlrd.book.Book object at 0x00000215C84C7B60>

获取工作表对象信息。代码如下:

python 复制代码
import xlrd
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
# 按照工作表的序号获取第一个工作表对象信息
sht1=excelbook.sheet_by_index(0)  
# 按照工作表的名称获取工作表对象信息
sht2=excelbook.sheet_by_name("总资产")
# 获取所有的工作表信息,保存到一个列表中。
shts=excelbook.sheets()
# 获取列表中的第二个元素,即第二个工作表对象信息
sht3=shts[1]
print(type(sht1))
print(sht1)
print(sht2)
print(type(shts))
print(shts)
print(sht3)

程序运行结果为:
<class 'xlrd.sheet.Sheet'>
<xlrd.sheet.Sheet object at 0x000001A8AB867E00>
<xlrd.sheet.Sheet object at 0x000001A8AB867E00>
<class 'list'>
[<xlrd.sheet.Sheet object at 0x000001A8AB867E00>, <xlrd.sheet.Sheet object at 0x000001A8AB94C2D0>]
<xlrd.sheet.Sheet object at 0x000001A8AB94C2D0>
2、读取单个单元格的信息

获取单元格数据有两种方式:一是使用cell_value函数直接返回单元格的数据,该函数传递两个参数:单元格数值所在的行号和列号。另一种是使用cell函数,该函数仍然需要传递两个参数:行号和列号,但该函数返回的是一个单元格对象,需要使用该对象的value属性返回单元格中的数据。

常用的单元格数据类型包含如下几种:

0:empty------空

1:string------文本

2:number------数值

3:date------日期

4:boolean------逻辑值

5:error------错误

6:blank------空格

例如:

python 复制代码
import xlrd
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
# 获取第一个工作表对象信息
sht1=excelbook.sheets()[0]
# 获取第3行第2列的单元格的内容
x=sht1.cell(2,1).value
print(x)
print(type(x))
# 获取第3行第6列的单元格的内容
x=sht1.cell_value(2,5)
print(x)
print(type(x))

程序运行结果为:
家具
<class 'str'>
700.0
<class 'float'>
3、读取多个单元格的信息

使用工作表的nrows属性可以获取工作表总的有效行数,ncols属性可以获取工作表总的有效列数。

python 复制代码
import xlrd
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
rows=excelbook.sheets()[0].nrows
cols=excelbook.sheets()[0].ncols
print(rows)
print(cols)

程序运行结果为:
7
9

使用row(n)函数可以获取第n行数据,使用col(n)函数可以获取第n列数据。

python 复制代码
import xlrd
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
row=excelbook.sheets()[0].row(2)
print(row)
print(type(row))
print("==="*20)
print(row[1])
print(type(row[1]))
print("==="*20)
col=excelbook.sheets()[0].col(3)
print(col)
print(type(col))

程序运行结果为:
[number:2.0, text:'家具', text:'JJ2020021492', text:'目录柜', text:'50斗', number:700.0, text:'经济与管理学院办公室', text:'10号教学楼2楼10206', xldate:33329.0]
<class 'list'>
============================================================
text:'家具'
<class 'xlrd.sheet.Cell'>
============================================================
[text:'名称', text:'期刊架', text:'目录柜', text:'双面书桌', text:'双面书桌', text:'双面书桌', text:'双面书桌']
<class 'list'>

row(n)函数与 col(n)函数获取到的数据是一个列表,列表中元素的数据类型为<class 'xlrd.sheet.Cell'>,这是xlrd中的cell对象。也就是每一个列表元素表示的是一个单元格对象,单元格对象有value属性。因此,可以遍历列表获得每一个单元格对象,再通过value属性获取每一个单元格的值。

代码如下:

python 复制代码
import xlrd
excelbook=xlrd.open_workbook(r"d:/资产清单.xlsx")
rows=excelbook.sheets()[0].nrows
for row_id in range(rows):
    for cell in excelbook.sheets()[0].row(row_id):
        print(cell.value,end="   ")
    print("")
    
程序运行结果为:
序号   类别   编号   名称   型号   价值   领用单位   存放地   入库日期   
1.0   家具   JJ2020019977   期刊架   D型   590.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
2.0   家具   JJ2020021492   目录柜   50斗   700.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
3.0   家具   JJ2020023833   双面书桌   双面   410.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
4.0   家具   JJ2020023834   双面书桌   双面   410.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
5.0   家具   JJ2020023835   双面书桌   双面   410.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
6.0   家具   JJ2020023836   双面书桌   双面   410.0   经济与管理学院办公室   10号教学楼2楼10206   33329.0   
相关推荐
JCGKS1 小时前
Go| excelize的流式迭代器
后端·golang·excel·excelize·流式读取·文件解析
大佬,救命!!!1 小时前
python实现五子棋
开发语言·python·个人开发·pygame·少儿编程·五子棋
明知道的博客4 小时前
解决WSL环境下DeepSeek-OCR运行时内存不足问题
python·ocr·deepseek·deepseek-ocr
FreeCode5 小时前
LangGraph1.0智能体开发:Graph API概念与设计
python·langchain·agent
test管家5 小时前
如何在Python中使用SQLite数据库进行增删改查操作?
python
yangmf20407 小时前
APM(三):监控 Python 服务链
大数据·运维·开发语言·python·elk·elasticsearch·搜索引擎
yangmf20407 小时前
APM(二):监控 Python 服务
大数据·python·elasticsearch·搜索引擎
CoderJia程序员甲7 小时前
GitHub 热榜项目 - 日榜(2025-11-23)
python·开源·github·mcp
AI爱好者7 小时前
WordPress保卫战:用Python分析日志并封禁恶意爬虫
python