-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathdocument_builder.py
More file actions
300 lines (267 loc) · 7.59 KB
/
document_builder.py
File metadata and controls
300 lines (267 loc) · 7.59 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
from jinja2 import Environment, PackageLoader
from pydocx.constants import EMUS_PER_PIXEL
templates = {
'delete': 'text_delete.xml',
'drawing': 'drawing.xml',
'hyperlink': 'hyperlink.xml',
'insert': 'insert.xml',
'linebreak': 'linebreak.xml',
'main': 'base.xml',
'numbering': 'numbering.xml',
'p': 'p.xml',
'pict': 'pict.xml',
'r': 'r.xml',
'rect': 'rect.xml',
'rpr': 'rpr.xml',
'sdt': 'sdt.xml',
'sectPr': 'sectPr.xml',
'smartTag': 'smart_tag.xml',
'style': 'style.xml',
'styles': 'styles.xml',
't': 't.xml',
'table': 'table.xml',
'tc': 'tc.xml',
'tr': 'tr.xml',
}
env = Environment(
loader=PackageLoader(
'tests',
'templates',
),
)
try:
string_types = (str, unicode)
except NameError:
string_types = str
def template_render(template, **render_args):
'''
Return a utf-8 bytestream of the rendered template
'''
return template.render(**render_args).encode('utf-8')
class DocxBuilder(object):
@classmethod
def xml(self, body):
template = env.get_template(templates['main'])
return template_render(
template,
body=body.decode('utf-8'),
)
@classmethod
def p_tag(
self,
text,
style='style0',
jc=None,
):
if isinstance(text, string_types):
# Use create a single r tag based on the text and the bold
run_tag = DocxBuilder.r_tag(
[DocxBuilder.t_tag(text)],
)
run_tags = [run_tag]
elif isinstance(text, list):
run_tags = text
else:
run_tags = [self.r_tag([])]
template = env.get_template(templates['p'])
return template_render(
template,
run_tags=run_tags,
style=style,
jc=jc,
)
@classmethod
def linebreak(self):
template = env.get_template(templates['linebreak'])
return template_render(template)
@classmethod
def t_tag(self, text):
template = env.get_template(templates['t'])
return template_render(template, text=text)
@classmethod
def r_tag(
self,
elements,
rpr=None,
):
template = env.get_template(templates['r'])
if rpr is None:
rpr = DocxBuilder.rpr_tag()
return template_render(
template,
elements=elements,
rpr=rpr,
)
@classmethod
def rpr_tag(self, inline_styles=None, *args, **kwargs):
if inline_styles is None:
inline_styles = {}
valid_styles = (
'b',
'i',
'u',
'caps',
'smallCaps',
'strike',
'dstrike',
'vanish',
'webHidden',
'vertAlign',
)
for key in inline_styles:
if key not in valid_styles:
raise AssertionError('%s is not a valid style' % key)
template = env.get_template(templates['rpr'])
return template_render(
template,
tags=inline_styles,
)
@classmethod
def hyperlink_tag(self, r_id, run_tags):
template = env.get_template(templates['hyperlink'])
return template_render(
template,
r_id=r_id,
run_tags=run_tags,
)
@classmethod
def insert_tag(self, run_tags):
template = env.get_template(templates['insert'])
return template_render(
template,
run_tags=run_tags,
)
@classmethod
def delete_tag(self, deleted_texts):
template = env.get_template(templates['delete'])
return template_render(
template,
deleted_texts=deleted_texts,
)
@classmethod
def smart_tag(self, run_tags):
template = env.get_template(templates['smartTag'])
return template_render(
template,
run_tags=run_tags,
)
@classmethod
def sdt_tag(self, p_tag):
template = env.get_template(templates['sdt'])
return template_render(
template,
p_tag=p_tag,
)
@classmethod
def li(self, text, ilvl, numId, bold=False):
if isinstance(text, list):
run_tags = []
for run_text, run_bold in text:
run_tags.append(
DocxBuilder.r_tag(
[DocxBuilder.t_tag(run_tags)],
run_bold,
),
)
else:
# Assume a string type
# Use create a single r tag based on the text and the bold
run_tag = DocxBuilder.r_tag([DocxBuilder.t_tag(text)], bold)
run_tags = [run_tag]
template = env.get_template(templates['p'])
return template_render(
template,
run_tags=run_tags,
is_list=True,
ilvl=ilvl,
numId=numId,
)
@classmethod
def table_cell(self, paragraph, merge=False, merge_continue=False, fill_color='auto'):
template = env.get_template(templates['tc'])
return template_render(
template,
paragraph=paragraph,
merge=merge,
merge_continue=merge_continue,
fill_color=fill_color
)
@classmethod
def table_row(self, tcs):
template = env.get_template(templates['tr'])
return template_render(
template,
table_cells=tcs,
)
@classmethod
def table(self, trs):
template = env.get_template(templates['table'])
return template_render(
template,
table_rows=trs,
)
@classmethod
def drawing(self, r_id, height=None, width=None):
template = env.get_template(templates['drawing'])
if height is not None:
height = height * EMUS_PER_PIXEL
if width is not None:
width = width * EMUS_PER_PIXEL
return template_render(
template,
r_id=r_id,
height=height,
width=width,
)
@classmethod
def pict(self, r_id=None, height=None, width=None):
template = env.get_template(templates['pict'])
return template_render(
template,
r_id=r_id,
height=height,
width=width,
)
@classmethod
def rect(self, r_id=None, height=None, width=None):
template = env.get_template(templates['rect'])
return template_render(
template,
r_id=r_id,
height=height,
width=width,
)
@classmethod
def sectPr_tag(self, p_tag):
template = env.get_template(templates['sectPr'])
return template_render(
template,
p_tag=p_tag,
)
@classmethod
def styles_xml(self, style_tags):
template = env.get_template(templates['styles'])
return template_render(
template,
style_tags=style_tags,
)
@classmethod
def style(self, style_id, value):
template = env.get_template(templates['style'])
return template_render(
template,
style_id=style_id,
value=value,
)
@classmethod
def numbering(self, numbering_dict):
template = env.get_template(templates['numbering'])
return template_render(
template,
numbering_dict=numbering_dict,
)