from openpyxl import load_workbook
def read_xlsx_to_dict_list(file_path_or_content):
workbook = None
if isinstance(file_path_or_content, str):
workbook = load_workbook(filename=file_path_or_content)
else:
from io import BytesIO
workbook = load_workbook(filename=BytesIO(file_path_or_content.read()))
sheet = workbook.active
header = [cell.value for cell in sheet[1]]
dict_list = []
for row in sheet.iter_rows(min_row=2, values_only=True):
row_dict = {header[i]: row[i] for i in range(len(header))}
dict_list.append(row_dict)
return dict_list
data = read_xlsx_to_dict_list('example.xlsx')
print(data)
从Excel文件读取数据到字典列表
最新推荐文章于 2024-07-25 10:31:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)