openpyxl读取Excle生成字典列表

openpyxl读取Excle生成字典列表

#!/user/bin/env python3
# -*- coding: utf-8 -*-
import openpyxl
import configparser, os

def excel_row_value_dice(file_name, title):
    wb = openpyxl.load_workbook(file_name)
    # ws = wb.active#打开当前页
    ws = wb[title]  # 打开指定页
    # 取出每行的值,以list方式存放
    rows_list = []
    for row in ws.rows:
        row_list = []
        for cell in row:
            row_list.append(cell.value)
        rows_list.append(row_list)
    # print(rows_list)
    # 结果转换成键值对的形式存放
    result = []
    for i in range(len(rows_list) - 1):
        row_dict = {}
        for j in range(len(rows_list[0])):
            row_dict[rows_list[0][j]] = rows_list[i + 1][j]
        result.append(row_dict)
    # print(reslut)
    return result
file_name, title=r'E:\新建 Microsoft Excel 工作表 (2).xlsx','Sheet1'
print(excel_row_value_dice(file_name, title))

你可能感兴趣的:(python3)