废话少说 直接上代码
python 3.7.9
import openpyxl
xlsxPath = './test.xlsx'
# 第一步打开工作簿
wb = openpyxl.load_workbook(xlsxPath)
# 第二步选取表单
sheet = wb.active
# 按行获取数据转换成列表
rows_data = list(sheet.rows)
# 获取表单的表头信息(第一行),也就是列表的第一个元素
titles = [title.value for title in rows_data.pop(0)]
print(titles)
整个表格最终转换出来的字典数据列表
all_row_dict = []
# 遍历出除了第一行的其他行
for a_row in rows_data:
the_row_data = [cell.value for cell in a_row]
# 将表头和该条数据内容,打包成一个字典
row_dict = dict(zip(titles, the_row_data))
print(row_dict)
all_row_dict.append(row_dict)
# print(all_row_dict)
结果例如: