-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_do_excel.py
More file actions
34 lines (30 loc) · 1015 Bytes
/
demo_do_excel.py
File metadata and controls
34 lines (30 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from openpyxl import load_workbook
def read_excel(path):
"""
读取excel
:param path: excel的路径
:return:
"""
wb = load_workbook(path)
# 获取多有的sheet的名字
sheet_names = wb.get_sheet_names()
print(sheet_names)
# 根据sheet名字获取sheet
info_sheet = wb.get_sheet_by_name(sheet_names[0])
# 获取工作簿的最大行数
# print(info_sheet.max_row)
# # 获取工作簿的最大列数
# print(info_sheet.max_column)
res = []
for i in range(2, info_sheet.max_row + 1):
info = {}
for j in range(1, info_sheet.max_column + 1):
value = info_sheet.cell(row=i, column=j).value
print("第{}行第{}列的数据是:{}".format(i, j, value))
# info_sheet.cell(row=1,column=j).value: key
info[info_sheet.cell(row=1, column=j).value] = value
res.append(info)
return res
info = read_excel("pyth_demo.xlsx")
print(info)
[{"name": "john", "age": 21}, {}, {}, {}]