python读取excel,返回dic列表

时间:2023-03-09 15:33:19
python读取excel,返回dic列表
def get_xls_sheets_as_dic(pro_name, xls_name):
dic_list = []
xls_path = os.path.join(BASE_PATH, "testFile", 'case', pro_name, xls_name)
file = open_workbook(xls_path)
sheets = file.sheets() for sheet in sheets:
nrows = sheet.nrows
ncols = sheet.ncols
dic_data = {}
for i in range(1, nrows):
for j in range(ncols):
title = sheet.cell_value(0, j)
value = sheet.cell_value(i, j)
dic_data[title] = str(value).replace('\n', '')
dic_list.append(dic_data)
return dic_list
def get_xls_sheet_by_name_as_dic(pro_name, xls_name, sheet_name):
dic_list = []
xls_path = os.path.join(BASE_PATH, "testFile", 'case', pro_name, xls_name)
file = open_workbook(xls_path)
sheet = file.sheet_by_name(sheet_name) nrows = sheet.nrows
ncols = sheet.ncols
dic_data = {}
for i in range(1, nrows):
for j in range(ncols):
title = sheet.cell_value(0, j)
value = sheet.cell_value(i, j)
dic_data[title] = str(value).replace('\n', '')
dic_list.append(dic_data)
return dic_list

参考:python 将excel里的内容转换为dict