1. 程式人生 > 其它 >【Python】讀取Excel檔案

【Python】讀取Excel檔案

技術標籤:pythonexcelpython

讀取excel表格需要的第三方庫:xlrd

安裝第三方庫:pip install xlrd

#匯入函式庫
import xlrd

workbook = xlrd.open_workbook("員工資訊.xls")

#第一個工作表sheet_by_index(0) 計算從0開始
sheet1 = workbook.sheet_by_index(0)

#共多少行
nrows = sheet1.nrows

#共有多少列
ncols = sheet1.ncols
row_values = sheet1.row_values(rowx=0)

#看看第一行(表頭)是什麼
print("共%d行%d列" % (nrows,ncols))
idx=0
for s in row_values:
    idx=idx+1
    print("第%d列=%s," %(idx,s))

#打每行的資料顯示出來
for i in range(1,nrows):
    #這個recode就是每行資料包裝在一起的陣列了
    record=sheet1.row_values(rowx=1)
    #可以單獨取某列的資料
    s1=record[1]
    print(s1)