-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_xlsx_fast.py
38 lines (34 loc) · 1.07 KB
/
template_xlsx_fast.py
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
35
36
37
38
#!/usr/bin/python3
# pip3 install openpyxl
import openpyxl
import os
def saveData(dataset_list, output_file):
output_file = "{}.xlsx".format(output_file)
fieldnames = ["Header 1", "Header 2", "Header 3",
"Header 4"] # change your header list here
if os.path.exists(output_file):
wb = openpyxl.load_workbook(output_file)
else:
wb = openpyxl.Workbook()
sheet = wb.active
last_row = sheet.max_row
if last_row == 1:
idx = 1
for fieldname in fieldnames:
sheet.cell(row=1, column=idx).value = fieldname
idx += 1
idx = 1
for dataset in dataset_list:
for data in dataset:
sheet.cell(row=last_row+1, column=idx).value = data
idx += 1
idx = 1
last_row += 1
wb.save(output_file)
if __name__ == "__main__":
data = ["test", "test1", "test2", "test3"]
data2 = ["test9", "test3", "test2", "test5"]
data_collection = []
data_collection.append(data)
data_collection.append(data2)
saveData(data_collection, "my_sheet")