-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield_values.py
More file actions
executable file
·289 lines (237 loc) · 9.09 KB
/
field_values.py
File metadata and controls
executable file
·289 lines (237 loc) · 9.09 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
#!/usr/bin/env python2
import zipfile
import datetime
import tempfile
import os
from lxml import etree as ET
from jellyfish import levenshtein_distance
# add more as needed
namespaces = {
'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
}
def log_warning(warning):
print "WARNING:", warning
def get_attr(element, attr):
index = '{%s}%s' % (namespaces['w'], attr)
if index not in element.attrib:
return None
return element.attrib[index]
def find_element_following_bookmark(
docxml,
start_selector,
check_element_lamdba,
failure_message='Could not find following node matching condition'):
"""
Finds the first sibling node matching check_element_lambda that follows the
first node found matching start_selector
"""
label_element = docxml.find(start_selector, namespaces)
if label_element is None:
raise Exception('Could not find name element with val=%s' % start_selector)
current_element = label_element
while (
current_element is not None and
check_element_lamdba(current_element)
):
current_element = current_element.getnext()
if current_element is None:
raise Exception(failure_message)
return current_element
def update_zip_in_place(
input_zipfile,
output_zipfile_name,
replaced_file_in_zip_name,
new_file_data
):
print "updating file", replaced_file_in_zip_name, 'in zipfile'
print "writing updated file to", output_zipfile_name
with zipfile.ZipFile(output_zipfile_name, 'w') as zout:
zout.comment = input_zipfile.comment # preserve the comment
# copy all files that are not the one we are replacing
for item in input_zipfile.infolist():
if item.filename != replaced_file_in_zip_name:
zout.writestr(item, input_zipfile.read(item.filename))
# now add replaced_file_in_zip_name with its new data
with zipfile.ZipFile(
output_zipfile_name,
mode='a',
compression=zipfile.ZIP_DEFLATED
) as cloned_zip_file:
cloned_zip_file.writestr(replaced_file_in_zip_name, new_file_data)
class Field(object):
def __init__(self, field_name, field_value):
self.field_name = field_name
self.field_value = field_value
def __repr__(self):
return "%s(%s, %s)" % (
type(self).__name__,
self.field_name.__repr__(),
self.field_value.__repr__()
)
class TextField(Field):
def __init__(self, field_name, field_value):
super(TextField, self).__init__(field_name, field_value)
def apply(self, docxml):
TextField.set_text_input(
docxml,
self.field_name,
self.field_value
)
@staticmethod
def set_text_input(docxml, name, value):
"""
Text inputs are a series of w:r elements at the same level bounded by
w:bookmarkStart and w:bookmarkEnd with common IDs.
"""
start_selector = './/w:bookmarkStart[@w:name="%s"]' % name
bookmark_start = docxml.find(start_selector, namespaces)
bookmark_id = get_attr(bookmark_start, 'id')
bookmark_end = docxml.find(
'.//w:bookmarkEnd[@w:id="%s"]' % (bookmark_id), namespaces)
current_element = bookmark_start
while (current_element != bookmark_end):
# print "stepping to next element"
current_element = current_element.getnext()
# Find any w:t elements and set their values.
text_input = current_element.find('.//w:t', namespaces)
if text_input is not None:
# print "updating current text element"
text_input.text = value
break
class CheckboxField(Field):
def __init__(self, field_name, field_value):
super(CheckboxField, self).__init__(
field_name,
CheckboxField.transform_value(field_value)
)
@staticmethod
def validate_value(field_value_string):
return CheckboxField.transform_value(field_value_string) is not None
@staticmethod
def transform_value(field_value_string):
field_value_lower = field_value_string.lower()
is_field_value_truthy = field_value_lower in [
'true', '1', 't', 'yes', 'y'
]
is_field_value_fasly = field_value_lower in [
'false', '0', 'f', 'no', 'n'
]
if (not is_field_value_truthy) and (not is_field_value_fasly):
return None
return is_field_value_truthy
def apply(self, docxml):
CheckboxField.check_checkbox(
docxml,
self.field_name,
self.field_value
)
@staticmethod
def check_checkbox(
docxml,
name,
should_be_checked
):
"""
Checkbox elements are w:checkBox element that are preceeded by a w:name
with val equal to the checkbox name
isChecked depends on the presence of an <w:checked> field inside this
checkBox element
"""
start_selector = './/w:name[@w:val="%s"]' % name
checkbox_tag_name = "{%s}checkBox" % namespaces['w']
checkbox_element = find_element_following_bookmark(
docxml,
start_selector,
lambda element: element.tag != checkbox_tag_name,
failure_message=(
"Could not find w:checkBox following a w:name with val=%s" % name
)
)
check_element = checkbox_element.find('w:checked', namespaces)
if should_be_checked and check_element is None:
new_check_element = ET.Element('{%s}checked' % namespaces['w'])
checkbox_element.append(new_check_element)
elif not should_be_checked and check_element is not None:
check_element.getparent().remove(check_element)
class DropdownField(Field):
def __init__(self, field_name, field_value):
super(DropdownField, self).__init__(field_name, field_value)
def apply(self, docxml):
DropdownField.select_option(docxml, self.field_name, self.field_value)
@staticmethod
def levenshtein_distance_nounicode(a, b):
return levenshtein_distance(
a.decode(encoding='UTF-8'),
b.decode(encoding='UTF-8')
)
@staticmethod
def get_best_option_index(options, requested):
best_option = min(
options,
key=lambda option: DropdownField.levenshtein_distance_nounicode(
requested, option)
)
distance_to_best_option = DropdownField.levenshtein_distance_nounicode(
requested,
best_option
)
if distance_to_best_option != 0:
log_warning(
"Using best match '%s' for requested option '%s'" % (
best_option,
requested,
)
)
return options.index(best_option)
@staticmethod
def select_option(docxml, name, option_to_select):
"""
Dropdown elements are a w:ddList element that are preceeded by a w:name
with val equal to the dropdown name
the selection depends on the presence of a <w:result w:val="$INDEX"/>
field inside this checkBox element
"""
start_selector = './/w:name[@w:val="%s"]' % name
dropdownlist_element_name = "{%s}ddList" % namespaces['w']
val_attribute_name = '{%s}val' % namespaces['w']
dropdown_element = find_element_following_bookmark(
docxml,
start_selector,
lambda element: element.tag != dropdownlist_element_name,
failure_message=(
"Could not find w:ddList following a w:name with val=%s" % name
)
)
dropdown_option_elements = dropdown_element.findall(
'{%s}listEntry' % namespaces['w']
)
dropdown_options = [
dropdown_option_element.attrib[val_attribute_name]
for dropdown_option_element
in dropdown_option_elements
]
index = DropdownField.get_best_option_index(
dropdown_options, option_to_select)
result_element = dropdown_element.find('{%s}result' % namespaces['w'])
if result_element is None:
result_element = ET.Element('{%s}result' % namespaces['w'])
dropdown_element.append(result_element)
result_element.attrib[val_attribute_name] = str(index)
def set_document_fields(in_name, out_name, fields):
# in_name = 'report_template.docx'
# out_name = 'report_template_out.docx'
input_zip_file = zipfile.ZipFile(in_name, 'r')
xml_text = input_zip_file.read('word/document.xml')
docx_namespace = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
ET.register_namespace('w', docx_namespace)
docxml = ET.fromstring(xml_text)
for field in fields:
field.apply(docxml)
xml_header = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
xml_body = ET.tostring(docxml)
update_zip_in_place(
input_zip_file,
out_name,
'word/document.xml',
xml_header + xml_body
)