Python之xlrd模块读取xls文件与报错

安装

pip3 install xlrd

用法

Sheet编号从0开始
rows,colnum编号均从0开始
合并的单元格仅返回第一格内容
Sheets只能被调用一次,可获取所有sheet取idx
无论表格内数据类型是int还是str,纯数字内容会返回float

示例

import xlrd
file = "name_list.xls"
data = xlrd.open_workbook(file)
table = data.sheets()#Read all sheet by idx

table_1 = table[1] #Input signal sheet
name = table_in.col_values(2, start_rowx=1, end_rowx=None) #Get col.C w/o header
print(name) #output ['Ada','Barry','Colin','Darwin']

table_2 = table[2] #Next sheet
...

注意

py文件名不可以命名为xlrd否则会与import冲突报错AttributeError: module 'xlrd' has no attribute 'open_workbook'

猜你喜欢

转载自www.cnblogs.com/azureology/p/12344428.html