-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (42 loc) · 1.16 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from docx import Document
def split_list(a_list, part_by):
"""
Split list into group of list
:param a_list: input one dimensional list
:param part_by: small list max length
:return:
"""
s_list = []
p_list = []
for item in a_list:
p_list.append(item)
if len(p_list) == part_by:
s_list.append(p_list)
p_list = []
if len(p_list) > 0:
s_list.append(p_list)
return s_list
# Read content from text file
filename = 'vocabulary.txt'
file = open(filename, 'r')
content = file.read()
file.close()
# Format the vocabularies
vocabularies = []
for s in content.splitlines():
vocabularies.append(s.strip())
vocabularies = list(reversed(vocabularies))
recordset = split_list(vocabularies, 2)
# Write the document
document = Document()
document.add_heading('Vocabulary', 0)
table = document.add_table(rows=len(recordset), cols=2)
for item in recordset:
row_cells = table.add_row().cells
col_count = len(item)
row_cells[0].text = str(item[0])
if col_count > 1:
row_cells[1].text = str(item[1])
print(item)
# document.add_page_break()
document.save('vocabulary.docx')