首先,安装xlrd,如果有源文件,直接在命令窗口切换到源文件中有setup.py的目录,输入python setup.py install,看到success就可以了,如果没有,可以在线下载pip install xlrd(确定有pip),或者使用命令easy_install install xlrd,此方法必须确定网络环境良好。
以下是 xlrd库的使用方法,
|
操作 |
解释 |
|
book.sheetnames() |
返回excel中所有sheets的名字 |
|
book.sheet_by_index(sheet_index) |
excel中的sheet索引从0开始,获取索引为sheet_index处的工作表对象 |
|
book.sheet_by_name(sheet_name) |
返回名字为sheet_name的工作表对象 |
|
book.sheets() |
返回excel中所有的sheets的对象,list类型 |
|
book.sheet_loaded(sheet_name_or_index = ‘Sheet1’) |
如果加载了sheets为‘Sheet1’的单元,则返回True;否则返回False,如果execl中不存在名为Sheet1的单元则抛出XLRDError |
|
book.unload_sheet(self, sheet_name_or_index) |
索引为index或者表名为name的工作表不能再使用 |
|
操作 |
返回值 |
解释 |
|
sheet.name |
str,sheet名字 |
获取工作表名称 |
|
sheet.nrows |
int,行数 |
获取工作表的行数 |
|
sheet.ncols |
int,列数 |
获取工作表的列数 |
|
sheet.row(rowx) |
list,xlrd.sheet.Cell |
获取rowx行的所有单元对象 |
|
sheet.row_values(num) |
list,第num行的值 |
获取工作表中第num行的内容 |
|
sheet.row_slice(rowx, start_colx=0, end_colx=None) |
list,xlrd.sheet.Cell |
获取rowx行中,strat_colx->end_colx内的单元 |
|
sheet.row_types(rowx, start_colx=0, end_colx=None) |
list |
获取rowx行中,strat_colx->end_colx内的单元类型 |
|
sheet.get_rows() |
generator |
所有行的生成器 |
|
sheet.col(colx) |
list,xlrd.sheet.Cell |
获取colx列的所有单元对象 |
|
sheet.col_values(num) |
list,第num列的值 |
获取工作表中第num列的内容 |
|
sheet.col_slice(colx,start_rowx=0, end_rowx=None) |
list,xlrd.sheet.Cell |
获取colx列中,strat_rowx->end_rowx内的单元 |
|
sheet.col_types(colx,start_rowx=0, end_rowx=None) |
list |
获取colx列中,strat_rowx->end_rowx内的单元类型 |
|
sheet.cell(rowx,colx) |
xlrd.sheet.Cell |
获取rowx行,colx列的单元对象 |
|
sheet.cell_value(rowx,colx) |
rowx行,colx列的值 |
获取rowx行,colx列的值 |
|
sheet.celltype(rowx,colx) |
返回单元的类型 |
获取(rowx,colx)处的类型 |
|
shell.put_cell(row, col, ctype, value, xf = 0) |
None |
修改(row,col)处的值,ctype为单元类型,(只是修改程序中此处的值,不会影响原文件) |
我使用了图形界面库简单实现了一个小功能,效果如下:



本文详细介绍xlrd库的安装及使用方法,包括如何获取Excel中的所有sheets名称、按索引或名称读取工作表、获取工作表的行数、列数及单元格值等。适合初学者快速上手。
498

被折叠的 条评论
为什么被折叠?



