安装:
pip install xlrd
简单使用:
import xlrd book = xlrd.open_workbook(r'C:\Users\dinghanhua\Desktop\yqqapi.xlsx') # 打开excel
print("the number of sheets:",book.nsheets) # sheet数量
print("sheet_names:",book.sheet_names()) # sheetname列表 sheet1 = book.sheet_by_index(0) # 通过索引取sheet
print(sheet1.name,sheet1.nrows,sheet1.ncols) #sheet名称、行数、列数
print(sheet1.cell(0,0).value) #cell值
print(sheet1.cell_value(0,1)) #cell值 sheet2 = book.sheet_by_name("sheet2") # 通过sheetname取sheet
print(sheet2.name,sheet2.nrows,sheet2.ncols) # 获取sheet所有的数据
for row in range(sheet1.nrows):
for col in range(sheet1.ncols):
print(sheet1.cell_value(row,col),end='\t')
print('\n') print(sheet1.col_values(0,1,sheet1.nrows)) # 获取第一列,第2行的所有值 print(sheet1.row(1)) # 获取第二行的值 for col in range(sheet1.ncols): # 按列获取值,每列是list
print(sheet1.col_values(col,0,sheet1.nrows)) for row in range(sheet1.nrows): # 按行获取值;每行都是list
print(sheet1.row_values(row,0,sheet1.ncols))
有的单元格带有左右空格,取值时用strip()处理下
cell = str(sheet1.cell_value(0,0)).strip()
the end!