python28 excel讀取模塊xlrd

安裝:

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))

 

 

the end!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章