forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparfmt.py
More file actions
313 lines (277 loc) · 9.23 KB
/
parfmt.py
File metadata and controls
313 lines (277 loc) · 9.23 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
# encoding: utf-8
"""
Custom element classes related to paragraph properties (CT_PPr).
"""
from ...enum.text import WD_ALIGN_PARAGRAPH, WD_LINE_SPACING
from ...shared import Length
from ..simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure
from ..xmlchemy import (
BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrOne
)
class CT_Ind(BaseOxmlElement):
"""
``<w:ind>`` element, specifying paragraph indentation.
"""
left = OptionalAttribute('w:left', ST_SignedTwipsMeasure)
right = OptionalAttribute('w:right', ST_SignedTwipsMeasure)
firstLine = OptionalAttribute('w:firstLine', ST_TwipsMeasure)
hanging = OptionalAttribute('w:hanging', ST_TwipsMeasure)
class CT_Jc(BaseOxmlElement):
"""
``<w:jc>`` element, specifying paragraph justification.
"""
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)
class CT_PPr(BaseOxmlElement):
"""
``<w:pPr>`` element, containing the properties for a paragraph.
"""
_tag_seq = (
'w:pStyle', 'w:keepNext', 'w:keepLines', 'w:pageBreakBefore',
'w:framePr', 'w:widowControl', 'w:numPr', 'w:suppressLineNumbers',
'w:pBdr', 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
'w:rPr', 'w:sectPr', 'w:pPrChange'
)
pStyle = ZeroOrOne('w:pStyle', successors=_tag_seq[1:])
keepNext = ZeroOrOne('w:keepNext', successors=_tag_seq[2:])
keepLines = ZeroOrOne('w:keepLines', successors=_tag_seq[3:])
pageBreakBefore = ZeroOrOne('w:pageBreakBefore', successors=_tag_seq[4:])
widowControl = ZeroOrOne('w:widowControl', successors=_tag_seq[6:])
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
ind = ZeroOrOne('w:ind', successors=_tag_seq[23:])
jc = ZeroOrOne('w:jc', successors=_tag_seq[27:])
sectPr = ZeroOrOne('w:sectPr', successors=_tag_seq[35:])
del _tag_seq
@property
def first_line_indent(self):
"""
A |Length| value calculated from the values of `w:ind/@w:firstLine`
and `w:ind/@w:hanging`. Returns |None| if the `w:ind` child is not
present.
"""
ind = self.ind
if ind is None:
return None
hanging = ind.hanging
if hanging is not None:
return Length(-hanging)
firstLine = ind.firstLine
if firstLine is None:
return None
return firstLine
@first_line_indent.setter
def first_line_indent(self, value):
if self.ind is None and value is None:
return
ind = self.get_or_add_ind()
ind.firstLine = ind.hanging = None
if value is None:
return
elif value < 0:
ind.hanging = -value
else:
ind.firstLine = value
@property
def ind_left(self):
"""
The value of `w:ind/@w:left` or |None| if not present.
"""
ind = self.ind
if ind is None:
return None
return ind.left
@ind_left.setter
def ind_left(self, value):
if value is None and self.ind is None:
return
ind = self.get_or_add_ind()
ind.left = value
@property
def ind_right(self):
"""
The value of `w:ind/@w:right` or |None| if not present.
"""
ind = self.ind
if ind is None:
return None
return ind.right
@ind_right.setter
def ind_right(self, value):
if value is None and self.ind is None:
return
ind = self.get_or_add_ind()
ind.right = value
@property
def jc_val(self):
"""
The value of the ``<w:jc>`` child element or |None| if not present.
"""
jc = self.jc
if jc is None:
return None
return jc.val
@jc_val.setter
def jc_val(self, value):
if value is None:
self._remove_jc()
return
self.get_or_add_jc().val = value
@property
def keepLines_val(self):
"""
The value of `keepLines/@val` or |None| if not present.
"""
keepLines = self.keepLines
if keepLines is None:
return None
return keepLines.val
@keepLines_val.setter
def keepLines_val(self, value):
if value is None:
self._remove_keepLines()
else:
self.get_or_add_keepLines().val = value
@property
def keepNext_val(self):
"""
The value of `keepNext/@val` or |None| if not present.
"""
keepNext = self.keepNext
if keepNext is None:
return None
return keepNext.val
@keepNext_val.setter
def keepNext_val(self, value):
if value is None:
self._remove_keepNext()
else:
self.get_or_add_keepNext().val = value
@property
def pageBreakBefore_val(self):
"""
The value of `pageBreakBefore/@val` or |None| if not present.
"""
pageBreakBefore = self.pageBreakBefore
if pageBreakBefore is None:
return None
return pageBreakBefore.val
@pageBreakBefore_val.setter
def pageBreakBefore_val(self, value):
if value is None:
self._remove_pageBreakBefore()
else:
self.get_or_add_pageBreakBefore().val = value
@property
def spacing_after(self):
"""
The value of `w:spacing/@w:after` or |None| if not present.
"""
spacing = self.spacing
if spacing is None:
return None
return spacing.after
@spacing_after.setter
def spacing_after(self, value):
if value is None and self.spacing is None:
return
self.get_or_add_spacing().after = value
@property
def spacing_before(self):
"""
The value of `w:spacing/@w:before` or |None| if not present.
"""
spacing = self.spacing
if spacing is None:
return None
return spacing.before
@spacing_before.setter
def spacing_before(self, value):
if value is None and self.spacing is None:
return
self.get_or_add_spacing().before = value
@property
def spacing_line(self):
"""
The value of `w:spacing/@w:line` or |None| if not present.
"""
spacing = self.spacing
if spacing is None:
return None
return spacing.line
@spacing_line.setter
def spacing_line(self, value):
if value is None and self.spacing is None:
return
self.get_or_add_spacing().line = value
@property
def spacing_lineRule(self):
"""
The value of `w:spacing/@w:lineRule` as a member of the
:ref:`WdLineSpacing` enumeration. Only the `MULTIPLE`, `EXACTLY`, and
`AT_LEAST` members are used. It is the responsibility of the client
to calculate the use of `SINGLE`, `DOUBLE`, and `MULTIPLE` based on
the value of `w:spacing/@w:line` if that behavior is desired.
"""
spacing = self.spacing
if spacing is None:
return None
lineRule = spacing.lineRule
if lineRule is None and spacing.line is not None:
return WD_LINE_SPACING.MULTIPLE
return lineRule
@spacing_lineRule.setter
def spacing_lineRule(self, value):
if value is None and self.spacing is None:
return
self.get_or_add_spacing().lineRule = value
@property
def style(self):
"""
String contained in <w:pStyle> child, or None if that element is not
present.
"""
pStyle = self.pStyle
if pStyle is None:
return None
return pStyle.val
@style.setter
def style(self, style):
"""
Set val attribute of <w:pStyle> child element to *style*, adding a
new element if necessary. If *style* is |None|, remove the <w:pStyle>
element if present.
"""
if style is None:
self._remove_pStyle()
return
pStyle = self.get_or_add_pStyle()
pStyle.val = style
@property
def widowControl_val(self):
"""
The value of `widowControl/@val` or |None| if not present.
"""
widowControl = self.widowControl
if widowControl is None:
return None
return widowControl.val
@widowControl_val.setter
def widowControl_val(self, value):
if value is None:
self._remove_widowControl()
else:
self.get_or_add_widowControl().val = value
class CT_Spacing(BaseOxmlElement):
"""
``<w:spacing>`` element, specifying paragraph spacing attributes such as
space before and line spacing.
"""
after = OptionalAttribute('w:after', ST_TwipsMeasure)
before = OptionalAttribute('w:before', ST_TwipsMeasure)
line = OptionalAttribute('w:line', ST_SignedTwipsMeasure)
lineRule = OptionalAttribute('w:lineRule', WD_LINE_SPACING)