python xlrd读取excel常用方法

转自:https://www.cnblogs.com/feiyueNotes/p/7786579.html

最近学习了python操作excel,记录下常用方法.

需要安装xlrd模块, 打开cmd,输入命令:pip install xlrd  进行安装,若已安装显示如下:

 xlrd读取excel常用方法如下:

'''一、打开文件'''
xl = xlrd.open_workbook(file)

'''二、获取sheet'''
print (xl.sheet_names())#获取sheet名称
print (xl.sheets())#获取sheet对象
print(xl.nsheets) #获取sheet总数
print(xl.sheet_by_name(u"目录"))
print (xl.sheet_by_index(1))


'''三、获取sheet内的汇总数据'''
table1 = xl.sheet_by_name(u"目录")
print(table1.name)
print (table1.ncols)
print(table1.nrows)

'''四、单元格批量读取'''
print(table1.row_values(0))#获取第n行的值 若是合并单元格 首行显示值 其它为空
print(table1.row(0))#获取值及类型
print (table1.row_types(0))
print(table1.col_values(0,1,4))#获取列,切片
print(table1.row_slice(1,0,2))

'''五、特定单元格读取'''
#取值
print(table1.cell(1,2).value)
print(table1.cell_value(1,2))
print(table1.row(1)[2]).value
print(table1.col(2)[1]).value
#取类型
print(table1.cell(1,2).ctype)
print(table1.cell_type(1,2))
print(table1.row(1)[2].ctype)

'''六、常用技巧(0,0)转换成A1'''
print(xlrd.cellname(0,0))
print(xlrd.cellnameabs(0,0))
print(xlrd.colname(0))

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/83108413
今日推荐