安裝 pip install xlrd
import xlrd
import os
curpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
#獲取excel路徑
excelPath = os.path.join(curpath,'common','hello.xlsx')
#excel文件的工作表名默認是'Sheet1''Sheet2'等,打算獲取工作表Sheet1里面的數據
sheetName = 'Sheet1'
#打開excel表
data = xlrd.open_workbook(excelPath)
#獲取excel的工作表Sheet1
table = data.sheet_by_name(sheetName)
#獲取Sheet1工作表中的第一行數據,一般是參數名
keys = table.row_values(0)
#獲取工作表中數據的行數
rows = table.nrows
#獲取工作表中數據的列數
cols = table.ncols
#取出excel表中的數據,放在一個list里面 list=[{'user':'11','psw':'1111','except':True},{'user':'22','psw':'22','except':True},...]
lt = []
for i in range(rows-1):
d = {}
#取出表里面的行數據,從第二行開始取值
row = table.row_values(i+1)
#將取出的每行中的每列數據,與第一行的參數名組成字典格式
for j in range(cols):
d[keys[j]] = row[j]
#將建好的字典添加到列表里面
lt.append(d)
#打印從excel取到的數據
print(lt)