python-xlrd常用方法

2019-01-24  本文已影响4人  忘了呼吸的那只猫

'''一、打开文件'''

xl = xlrd.open_workbook(file)

'''二、获取sheet'''

print (xl.sheet_names())#获取全部sheet名称返回一个列表

print (xl.sheets())               #  获取sheet对象
print(xl.sheet_by_name(u"目录名"))
print (xl.sheet_by_index(1))

print(xl.nsheets) #获取sheet总数

'''三、获取sheet内的汇总数据'''

table1 = xl.sheet_by_name(u"目录名") 
print(table1.name) 获取sheet名
print (table1.ncols)获取sheet列数
print(table1.nrows)获取sheet行数

'''四、单元格批量读取'''

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))
上一篇 下一篇

猜你喜欢

热点阅读