-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfillTemplate.py
86 lines (74 loc) · 2.33 KB
/
fillTemplate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import docx
# Defince test data
testData = {
'name': 'John Doe',
'age': 42,
'address': '123 Main Street',
'city': 'Anytown'
}
# define test data for table
testDataRows = [
{
'name': 'John Doe',
'age': 42,
'address': '123 Main Street'
},
{
'name': 'Martin Gomez',
'age': 32,
'address': '456 Main Street'
},
{
'name': 'Peter Parker',
'age': 36,
'address': '456 Main Street'
},
{
'name': 'Leon Kennedy',
'age': 29,
'address': '456 Main Street'
},
]
class DocxTemplate:
def __init__(self, templatePath):
self.templatePath = templatePath
self.doc = docx.Document(templatePath)
def fillTemplate(self, data):
for paragraph in self.doc.paragraphs:
inline = paragraph.runs
for i in range(len(inline)):
inline[i].text = self.replaceText(inline[i].text, data)
def getTables(self):
ret = []
for table in self.doc.tables:
if len(table.rows) >= 2:
ret.append({'type': table.rows[2].cells[0].text, 'object': table})
return ret
def fillTable(self, table, data):
# save cell text to list
cellText = []
for cell in table.rows[1].cells:
cellText.append(cell.text)
# Remove first row (template row)
table._tbl.remove(table.rows[2]._tr)
table._tbl.remove(table.rows[1]._tr)
# Add new rows
for dataRow in data:
table.add_row()
# Add data to cells
for i in range(len(cellText)):
table.rows[-1].cells[i].text = self.replaceText(cellText[i], dataRow)
def replaceText(self, templateText, data):
for key in data:
if '${' + key + '}' in templateText:
templateText = templateText.replace('${' + key + '}', str(data[key]))
return templateText
def save(self, path):
self.doc.save(path)
docTemplate = DocxTemplate('template.docx')
tables = docTemplate.getTables()
for table in tables:
print('found table: ' + table['type'])
docTemplate.fillTable(table['object'], testDataRows)
docTemplate.fillTemplate(testData)
docTemplate.save('outputDoc.docx')