打开excel
import xlrd
x1 = xlrd.open_workbook("1.xls")
获取所有sheet名字
names=x1.sheet_names()
返回值
- list类型
打印名字:
print(names)
获取sheet数量
num=x1.nsheets
print("数量:",num)
通过名字获取sheet对象
sheet=x1.sheet_by_name("Sheet1")
print("名字:",sheet.name)
print("总行数:",sheet.nrows)
print("总列数:",sheet.ncols)
print("========================")
获取sheet名字、总行数、总列数
根据sheet对象获取信息:
获取sheet名:sheet.name
获取总行数:sheet.nrows
获取总列数:sheet.ncols
通过索引获取sheet对象
sheet=x1.sheet_by_index(0)
注意: index是索引,从0开始
print("名字:",sheet.name)
print("总行数:",sheet.nrows)
print("总列数:",sheet.ncols)
获取所有sheet对象
print("遍历excel=======================")
sheets=x1.sheets()
根据sheet对象获取信息
for item in sheets:
print("名字:",item.name)
print("总行数:",item.nrows)
print("总列数:",item.ncols)