forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveToExcel.py
More file actions
43 lines (35 loc) · 1.28 KB
/
Copy pathSaveToExcel.py
File metadata and controls
43 lines (35 loc) · 1.28 KB
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
39
40
41
42
43
from openpyxl import Workbook
from openpyxl import load_workbook
path_save = "/home/lwh/SublimeTextProject/test.xlsx"
path_read = "/home/lwh/SublimeTextProject/demo.txt"
def write_test():
wb = Workbook() # 1.creat a work book
wb_sheet0 = wb.create_sheet("student") # 2.create a sheet in the work book
datas_str = ""
datas_dict = {}
with open(path_read, "r") as f:
lines = f.readlines()
# process the data from txt file.I make it be a dict
for line in lines:
if len(line.strip()) > 1:
key, value = line.split(':')
key = key.strip().strip("\"")
value = value.strip().strip(",").strip("[").strip("]")
value_list = value.split(",")
# print(len(value_list))
datas_dict[key] = value_list
for i in range(1, 4): # 3.assign value to cell of the sheet
for j in range(1, 5):
cell = wb_sheet0.cell(row=i, column=j + 1)
cell.value = datas_dict[str(i)][j - 1]
# print(cell.value)
cell = wb_sheet0.cell(row=i, column=1)
cell.value = i
'''
for row in wb_sheet0.iter_rows():
for cell in row:
print(cell.value)
'''
wb.save(path_save) # 4.save the file
print('success')
write_test()