forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.py
More file actions
330 lines (286 loc) · 9.35 KB
/
text.py
File metadata and controls
330 lines (286 loc) · 9.35 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# encoding: utf-8
"""
Text-related proxy types for python-docx, such as Paragraph and Run.
"""
from __future__ import absolute_import, print_function, unicode_literals
from docx.enum.text import WD_BREAK
def boolproperty(f):
"""
@boolproperty decorator. Decorated method must return the XML element
name of the boolean property element occuring under rPr. Causes
a read/write tri-state property to be added to the class having the name
of the decorated function.
"""
def _get_prop_value(parent, attr_name):
return getattr(parent, attr_name)
def _remove_prop(parent, attr_name):
remove_method_name = 'remove_%s' % attr_name
remove_method = getattr(parent, remove_method_name)
remove_method()
def _add_prop(parent, attr_name):
add_method_name = 'add_%s' % attr_name
add_method = getattr(parent, add_method_name)
return add_method()
def getter(obj):
r, attr_name = obj._r, f(obj)
if r.rPr is None:
return None
prop_value = _get_prop_value(r.rPr, attr_name)
if prop_value is None:
return None
return prop_value.val
def setter(obj, value):
r, attr_name = obj._r, f(obj)
rPr = r.get_or_add_rPr()
_remove_prop(rPr, attr_name)
if value is not None:
elm = _add_prop(rPr, attr_name)
if bool(value) is False:
elm.val = False
return property(getter, setter, doc=f.__doc__)
class Paragraph(object):
"""
Proxy object wrapping ``<w:p>`` element.
"""
def __init__(self, p):
super(Paragraph, self).__init__()
self._p = p
def add_run(self, text=None):
"""
Append a run to this paragraph.
"""
r = self._p.add_r()
run = Run(r)
if text:
run.add_text(text)
return run
@property
def runs(self):
"""
Sequence of |Run| instances corresponding to the <w:r> elements in
this paragraph.
"""
return [Run(r) for r in self._p.r_lst]
@property
def style(self):
"""
Paragraph style for this paragraph. Read/Write.
"""
style = self._p.style
return style if style is not None else 'Normal'
@style.setter
def style(self, style):
self._p.style = None if style == 'Normal' else style
@property
def text(self):
"""
A string formed by concatenating the text of each run in the
paragraph.
"""
text = ''
for run in self.runs:
text += run.text
return text
class Run(object):
"""
Proxy object wrapping ``<w:r>`` element. Several of the properties on Run
take a tri-state value, |True|, |False|, or |None|. |True| and |False|
correspond to on and off respectively. |None| indicates the property is
not specified directly on the run and its effective value is taken from
the style hierarchy.
"""
def __init__(self, r):
super(Run, self).__init__()
self._r = r
def add_break(self, break_type=WD_BREAK.LINE):
"""
Add a break element of *break_type* to this run. *break_type* can
take the values `WD_BREAK.LINE`, `WD_BREAK.PAGE`, and
`WD_BREAK.COLUMN` where `WD_BREAK` is imported from `docx.enum.text`.
*break_type* defaults to `WD_BREAK.LINE`.
"""
type_, clear = {
WD_BREAK.LINE: (None, None),
WD_BREAK.PAGE: ('page', None),
WD_BREAK.COLUMN: ('column', None),
WD_BREAK.LINE_CLEAR_LEFT: ('textWrapping', 'left'),
WD_BREAK.LINE_CLEAR_RIGHT: ('textWrapping', 'right'),
WD_BREAK.LINE_CLEAR_ALL: ('textWrapping', 'all'),
}[break_type]
br = self._r.add_br()
if type_ is not None:
br.type = type_
if clear is not None:
br.clear = clear
def add_text(self, text):
"""
Add a text element to this run.
"""
t = self._r.add_t(text)
return Text(t)
@boolproperty
def all_caps(self):
"""
Read/write. Causes the text of the run to appear in capital letters.
"""
return 'caps'
@boolproperty
def bold(self):
"""
Read/write. Causes the text of the run to appear in bold.
"""
return 'b'
@boolproperty
def complex_script(self):
"""
Read/write tri-state value. When |True|, causes the characters in the
run to be treated as complex script regardless of their Unicode
values.
"""
return 'cs'
@boolproperty
def cs_bold(self):
"""
Read/write tri-state value. When |True|, causes the complex script
characters in the run to be displayed in bold typeface.
"""
return 'bCs'
@boolproperty
def cs_italic(self):
"""
Read/write tri-state value. When |True|, causes the complex script
characters in the run to be displayed in italic typeface.
"""
return 'iCs'
@boolproperty
def double_strike(self):
"""
Read/write tri-state value. When |True|, causes the text in the run
to appear with double strikethrough.
"""
return 'dstrike'
@boolproperty
def emboss(self):
"""
Read/write tri-state value. When |True|, causes the text in the run
to appear as if raised off the page in relief.
"""
return 'emboss'
@boolproperty
def hidden(self):
"""
Read/write tri-state value. When |True|, causes the text in the run
to be hidden from display, unless applications settings force hidden
text to be shown.
"""
return 'vanish'
@boolproperty
def italic(self):
"""
Read/write tri-state value. When |True|, causes the text of the run
to appear in italics.
"""
return 'i'
@boolproperty
def imprint(self):
"""
Read/write tri-state value. When |True|, causes the text in the run
to appear as if pressed into the page.
"""
return 'imprint'
@boolproperty
def math(self):
"""
Read/write tri-state value. When |True|, specifies this run contains
WML that should be handled as though it was Office Open XML Math.
"""
return 'oMath'
@boolproperty
def no_proof(self):
"""
Read/write tri-state value. When |True|, specifies that the contents
of this run should not report any errors when the document is scanned
for spelling and grammar.
"""
return 'noProof'
@boolproperty
def outline(self):
"""
Read/write tri-state value. When |True| causes the characters in the
run to appear as if they have an outline, by drawing a one pixel wide
border around the inside and outside borders of each character glyph.
"""
return 'outline'
@boolproperty
def rtl(self):
"""
Read/write tri-state value. When |True| causes the text in the run
to have right-to-left characteristics.
"""
return 'rtl'
@boolproperty
def shadow(self):
"""
Read/write tri-state value. When |True| causes the text in the run
to appear as if each character has a shadow.
"""
return 'shadow'
@boolproperty
def small_caps(self):
"""
Read/write tri-state value. When |True| causes the lowercase
characters in the run to appear as capital letters two points smaller
than the font size specified for the run.
"""
return 'smallCaps'
@boolproperty
def snap_to_grid(self):
"""
Read/write tri-state value. When |True| causes the run to use the
document grid characters per line settings defined in the docGrid
element when laying out the characters in this run.
"""
return 'snapToGrid'
@boolproperty
def spec_vanish(self):
"""
Read/write tri-state value. When |True|, specifies that the given run
shall always behave as if it is hidden, even when hidden text is
being displayed in the current document. The property has a very
narrow, specialized use related to the table of contents. Consult the
spec (§17.3.2.36) for more details.
"""
return 'specVanish'
@boolproperty
def strike(self):
"""
Read/write tri-state value. When |True| causes the text in the run
to appear with a single horizontal line through the center of the
line.
"""
return 'strike'
@property
def text(self):
"""
A string formed by concatenating all the <w:t> elements present in
this run.
"""
text = ''
for t in self._r.t_lst:
text += t.text
return text
@boolproperty
def web_hidden(self):
"""
Read/write tri-state value. When |True|, specifies that the contents
of this run shall be hidden when the document is displayed in web
page view.
"""
return 'webHidden'
class Text(object):
"""
Proxy object wrapping ``<w:t>`` element.
"""
def __init__(self, t_elm):
super(Text, self).__init__()
self._t = t_elm