forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.py
More file actions
264 lines (229 loc) · 7.67 KB
/
section.py
File metadata and controls
264 lines (229 loc) · 7.67 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
# encoding: utf-8
"""
Section-related custom element classes.
"""
from __future__ import absolute_import, print_function
from copy import deepcopy
from ..enum.section import WD_ORIENTATION, WD_SECTION_START
from .simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure
from .xmlchemy import BaseOxmlElement, OptionalAttribute, ZeroOrOne
class CT_PageMar(BaseOxmlElement):
"""
``<w:pgMar>`` element, defining page margins.
"""
top = OptionalAttribute('w:top', ST_SignedTwipsMeasure)
right = OptionalAttribute('w:right', ST_TwipsMeasure)
bottom = OptionalAttribute('w:bottom', ST_SignedTwipsMeasure)
left = OptionalAttribute('w:left', ST_TwipsMeasure)
header = OptionalAttribute('w:header', ST_TwipsMeasure)
footer = OptionalAttribute('w:footer', ST_TwipsMeasure)
gutter = OptionalAttribute('w:gutter', ST_TwipsMeasure)
class CT_PageSz(BaseOxmlElement):
"""
``<w:pgSz>`` element, defining page dimensions and orientation.
"""
w = OptionalAttribute('w:w', ST_TwipsMeasure)
h = OptionalAttribute('w:h', ST_TwipsMeasure)
orient = OptionalAttribute(
'w:orient', WD_ORIENTATION, default=WD_ORIENTATION.PORTRAIT
)
class CT_SectPr(BaseOxmlElement):
"""
``<w:sectPr>`` element, the container element for section properties.
"""
__child_sequence__ = (
'w:footnotePr', 'w:endnotePr', 'w:type', 'w:pgSz', 'w:pgMar',
'w:paperSrc', 'w:pgBorders', 'w:lnNumType', 'w:pgNumType', 'w:cols',
'w:formProt', 'w:vAlign', 'w:noEndnote', 'w:titlePg',
'w:textDirection', 'w:bidi', 'w:rtlGutter', 'w:docGrid',
'w:printerSettings', 'w:sectPrChange',
)
type = ZeroOrOne('w:type', successors=(
__child_sequence__[__child_sequence__.index('w:type')+1:]
))
pgSz = ZeroOrOne('w:pgSz', successors=(
__child_sequence__[__child_sequence__.index('w:pgSz')+1:]
))
pgMar = ZeroOrOne('w:pgMar', successors=(
__child_sequence__[__child_sequence__.index('w:pgMar')+1:]
))
@property
def bottom_margin(self):
"""
The value of the ``w:bottom`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.bottom
@bottom_margin.setter
def bottom_margin(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.bottom = value
def clone(self):
"""
Return an exact duplicate of this ``<w:sectPr>`` element tree
suitable for use in adding a section break. All rsid* attributes are
removed from the root ``<w:sectPr>`` element.
"""
clone_sectPr = deepcopy(self)
clone_sectPr.attrib.clear()
return clone_sectPr
@property
def footer(self):
"""
The value of the ``w:footer`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.footer
@footer.setter
def footer(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.footer = value
@property
def gutter(self):
"""
The value of the ``w:gutter`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.gutter
@gutter.setter
def gutter(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.gutter = value
@property
def header(self):
"""
The value of the ``w:header`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.header
@header.setter
def header(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.header = value
@property
def left_margin(self):
"""
The value of the ``w:left`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.left
@left_margin.setter
def left_margin(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.left = value
@property
def right_margin(self):
"""
The value of the ``w:right`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.right
@right_margin.setter
def right_margin(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.right = value
@property
def orientation(self):
"""
The member of the ``WD_ORIENTATION`` enumeration corresponding to the
value of the ``orient`` attribute of the ``<w:pgSz>`` child element,
or ``WD_ORIENTATION.PORTRAIT`` if not present.
"""
pgSz = self.pgSz
if pgSz is None:
return WD_ORIENTATION.PORTRAIT
return pgSz.orient
@orientation.setter
def orientation(self, value):
pgSz = self.get_or_add_pgSz()
pgSz.orient = value
@property
def page_height(self):
"""
Value in EMU of the ``h`` attribute of the ``<w:pgSz>`` child
element, or |None| if not present.
"""
pgSz = self.pgSz
if pgSz is None:
return None
return pgSz.h
@page_height.setter
def page_height(self, value):
pgSz = self.get_or_add_pgSz()
pgSz.h = value
@property
def page_width(self):
"""
Value in EMU of the ``w`` attribute of the ``<w:pgSz>`` child
element, or |None| if not present.
"""
pgSz = self.pgSz
if pgSz is None:
return None
return pgSz.w
@page_width.setter
def page_width(self, value):
pgSz = self.get_or_add_pgSz()
pgSz.w = value
@property
def start_type(self):
"""
The member of the ``WD_SECTION_START`` enumeration corresponding to
the value of the ``val`` attribute of the ``<w:type>`` child element,
or ``WD_SECTION_START.NEW_PAGE`` if not present.
"""
type = self.type
if type is None or type.val is None:
return WD_SECTION_START.NEW_PAGE
return type.val
@start_type.setter
def start_type(self, value):
if value is None or value is WD_SECTION_START.NEW_PAGE:
self._remove_type()
return
type = self.get_or_add_type()
type.val = value
@property
def top_margin(self):
"""
The value of the ``w:top`` attribute in the ``<w:pgMar>`` child
element, as a |Length| object, or |None| if either the element or the
attribute is not present.
"""
pgMar = self.pgMar
if pgMar is None:
return None
return pgMar.top
@top_margin.setter
def top_margin(self, value):
pgMar = self.get_or_add_pgMar()
pgMar.top = value
class CT_SectType(BaseOxmlElement):
"""
``<w:sectType>`` element, defining the section start type.
"""
val = OptionalAttribute('w:val', WD_SECTION_START)