使用openpyxl库读取excel中的数据
"""
测试数据处理模块
"""
from openpyxl import load_workbook
def get_data_from_excel(file, sheet_name=None):
"""
获取excel文件中指定表里的数据
:param file:
:param sheet_name:
:return:
"""
#读取excel文件
wb = load_workbook(file)
#读取对应表
if sheet_name is None:
ws = wb.active
else:
ws = wb[sheet_name]
#创建一个列表容器存放数据
data = []
#获取表头,作为字典的key
row_list = list(ws.rows)
title = [item.value for item in row_list[0]]
#获取其他数据
for row in row_list[1:]:
#获取每一行的数据
temp = [i.value for i in row]
#将表头与这一行数据打包成字典
data.append(dict(zip(title, temp)))
return data
if __name__ == '__main__':
res = get_data_from_excel(r"E:/test/test.xlsx")
# print(res)