Skip to content

Commit 5e9c955

Browse files
author
Steve Canny
committed
reorg: extract docx.oxml.text.parfmt
1 parent 83cf975 commit 5e9c955

File tree

3 files changed

+318
-310
lines changed

3 files changed

+318
-310
lines changed

docx/oxml/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,14 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
174174
register_element_cls('w:vertAlign', CT_VerticalAlignRun)
175175
register_element_cls('w:webHidden', CT_OnOff)
176176

177-
from .text.paragraph import CT_Ind, CT_Jc, CT_P, CT_PPr, CT_Spacing
177+
from .text.paragraph import CT_P
178+
register_element_cls('w:p', CT_P)
179+
180+
from .text.parfmt import CT_Ind, CT_Jc, CT_PPr, CT_Spacing
178181
register_element_cls('w:ind', CT_Ind)
179182
register_element_cls('w:jc', CT_Jc)
180183
register_element_cls('w:keepLines', CT_OnOff)
181184
register_element_cls('w:keepNext', CT_OnOff)
182-
register_element_cls('w:p', CT_P)
183185
register_element_cls('w:pageBreakBefore', CT_OnOff)
184186
register_element_cls('w:pPr', CT_PPr)
185187
register_element_cls('w:pStyle', CT_String)

docx/oxml/text/paragraph.py

Lines changed: 1 addition & 308 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,8 @@
44
Custom element classes related to paragraphs (CT_P).
55
"""
66

7-
from ...enum.text import WD_ALIGN_PARAGRAPH, WD_LINE_SPACING
87
from ..ns import qn
9-
from ...shared import Length
10-
from ..simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure
11-
from ..xmlchemy import (
12-
BaseOxmlElement, OptionalAttribute, OxmlElement, RequiredAttribute,
13-
ZeroOrMore, ZeroOrOne
14-
)
15-
16-
17-
class CT_Ind(BaseOxmlElement):
18-
"""
19-
``<w:ind>`` element, specifying paragraph indentation.
20-
"""
21-
left = OptionalAttribute('w:left', ST_SignedTwipsMeasure)
22-
right = OptionalAttribute('w:right', ST_SignedTwipsMeasure)
23-
firstLine = OptionalAttribute('w:firstLine', ST_TwipsMeasure)
24-
hanging = OptionalAttribute('w:hanging', ST_TwipsMeasure)
25-
26-
27-
class CT_Jc(BaseOxmlElement):
28-
"""
29-
``<w:jc>`` element, specifying paragraph justification.
30-
"""
31-
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)
8+
from ..xmlchemy import BaseOxmlElement, OxmlElement, ZeroOrMore, ZeroOrOne
329

3310

3411
class CT_P(BaseOxmlElement):
@@ -99,287 +76,3 @@ def style(self):
9976
def style(self, style):
10077
pPr = self.get_or_add_pPr()
10178
pPr.style = style
102-
103-
104-
class CT_PPr(BaseOxmlElement):
105-
"""
106-
``<w:pPr>`` element, containing the properties for a paragraph.
107-
"""
108-
_tag_seq = (
109-
'w:pStyle', 'w:keepNext', 'w:keepLines', 'w:pageBreakBefore',
110-
'w:framePr', 'w:widowControl', 'w:numPr', 'w:suppressLineNumbers',
111-
'w:pBdr', 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
112-
'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
113-
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
114-
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
115-
'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
116-
'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
117-
'w:rPr', 'w:sectPr', 'w:pPrChange'
118-
)
119-
pStyle = ZeroOrOne('w:pStyle', successors=_tag_seq[1:])
120-
keepNext = ZeroOrOne('w:keepNext', successors=_tag_seq[2:])
121-
keepLines = ZeroOrOne('w:keepLines', successors=_tag_seq[3:])
122-
pageBreakBefore = ZeroOrOne('w:pageBreakBefore', successors=_tag_seq[4:])
123-
widowControl = ZeroOrOne('w:widowControl', successors=_tag_seq[6:])
124-
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
125-
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
126-
ind = ZeroOrOne('w:ind', successors=_tag_seq[23:])
127-
jc = ZeroOrOne('w:jc', successors=_tag_seq[27:])
128-
sectPr = ZeroOrOne('w:sectPr', successors=_tag_seq[35:])
129-
del _tag_seq
130-
131-
@property
132-
def first_line_indent(self):
133-
"""
134-
A |Length| value calculated from the values of `w:ind/@w:firstLine`
135-
and `w:ind/@w:hanging`. Returns |None| if the `w:ind` child is not
136-
present.
137-
"""
138-
ind = self.ind
139-
if ind is None:
140-
return None
141-
hanging = ind.hanging
142-
if hanging is not None:
143-
return Length(-hanging)
144-
firstLine = ind.firstLine
145-
if firstLine is None:
146-
return None
147-
return firstLine
148-
149-
@first_line_indent.setter
150-
def first_line_indent(self, value):
151-
if self.ind is None and value is None:
152-
return
153-
ind = self.get_or_add_ind()
154-
ind.firstLine = ind.hanging = None
155-
if value is None:
156-
return
157-
elif value < 0:
158-
ind.hanging = -value
159-
else:
160-
ind.firstLine = value
161-
162-
@property
163-
def ind_left(self):
164-
"""
165-
The value of `w:ind/@w:left` or |None| if not present.
166-
"""
167-
ind = self.ind
168-
if ind is None:
169-
return None
170-
return ind.left
171-
172-
@ind_left.setter
173-
def ind_left(self, value):
174-
if value is None and self.ind is None:
175-
return
176-
ind = self.get_or_add_ind()
177-
ind.left = value
178-
179-
@property
180-
def ind_right(self):
181-
"""
182-
The value of `w:ind/@w:right` or |None| if not present.
183-
"""
184-
ind = self.ind
185-
if ind is None:
186-
return None
187-
return ind.right
188-
189-
@ind_right.setter
190-
def ind_right(self, value):
191-
if value is None and self.ind is None:
192-
return
193-
ind = self.get_or_add_ind()
194-
ind.right = value
195-
196-
@property
197-
def jc_val(self):
198-
"""
199-
The value of the ``<w:jc>`` child element or |None| if not present.
200-
"""
201-
jc = self.jc
202-
if jc is None:
203-
return None
204-
return jc.val
205-
206-
@jc_val.setter
207-
def jc_val(self, value):
208-
if value is None:
209-
self._remove_jc()
210-
return
211-
self.get_or_add_jc().val = value
212-
213-
@property
214-
def keepLines_val(self):
215-
"""
216-
The value of `keepLines/@val` or |None| if not present.
217-
"""
218-
keepLines = self.keepLines
219-
if keepLines is None:
220-
return None
221-
return keepLines.val
222-
223-
@keepLines_val.setter
224-
def keepLines_val(self, value):
225-
if value is None:
226-
self._remove_keepLines()
227-
else:
228-
self.get_or_add_keepLines().val = value
229-
230-
@property
231-
def keepNext_val(self):
232-
"""
233-
The value of `keepNext/@val` or |None| if not present.
234-
"""
235-
keepNext = self.keepNext
236-
if keepNext is None:
237-
return None
238-
return keepNext.val
239-
240-
@keepNext_val.setter
241-
def keepNext_val(self, value):
242-
if value is None:
243-
self._remove_keepNext()
244-
else:
245-
self.get_or_add_keepNext().val = value
246-
247-
@property
248-
def pageBreakBefore_val(self):
249-
"""
250-
The value of `pageBreakBefore/@val` or |None| if not present.
251-
"""
252-
pageBreakBefore = self.pageBreakBefore
253-
if pageBreakBefore is None:
254-
return None
255-
return pageBreakBefore.val
256-
257-
@pageBreakBefore_val.setter
258-
def pageBreakBefore_val(self, value):
259-
if value is None:
260-
self._remove_pageBreakBefore()
261-
else:
262-
self.get_or_add_pageBreakBefore().val = value
263-
264-
@property
265-
def spacing_after(self):
266-
"""
267-
The value of `w:spacing/@w:after` or |None| if not present.
268-
"""
269-
spacing = self.spacing
270-
if spacing is None:
271-
return None
272-
return spacing.after
273-
274-
@spacing_after.setter
275-
def spacing_after(self, value):
276-
if value is None and self.spacing is None:
277-
return
278-
self.get_or_add_spacing().after = value
279-
280-
@property
281-
def spacing_before(self):
282-
"""
283-
The value of `w:spacing/@w:before` or |None| if not present.
284-
"""
285-
spacing = self.spacing
286-
if spacing is None:
287-
return None
288-
return spacing.before
289-
290-
@spacing_before.setter
291-
def spacing_before(self, value):
292-
if value is None and self.spacing is None:
293-
return
294-
self.get_or_add_spacing().before = value
295-
296-
@property
297-
def spacing_line(self):
298-
"""
299-
The value of `w:spacing/@w:line` or |None| if not present.
300-
"""
301-
spacing = self.spacing
302-
if spacing is None:
303-
return None
304-
return spacing.line
305-
306-
@spacing_line.setter
307-
def spacing_line(self, value):
308-
if value is None and self.spacing is None:
309-
return
310-
self.get_or_add_spacing().line = value
311-
312-
@property
313-
def spacing_lineRule(self):
314-
"""
315-
The value of `w:spacing/@w:lineRule` as a member of the
316-
:ref:`WdLineSpacing` enumeration. Only the `MULTIPLE`, `EXACTLY`, and
317-
`AT_LEAST` members are used. It is the responsibility of the client
318-
to calculate the use of `SINGLE`, `DOUBLE`, and `MULTIPLE` based on
319-
the value of `w:spacing/@w:line` if that behavior is desired.
320-
"""
321-
spacing = self.spacing
322-
if spacing is None:
323-
return None
324-
lineRule = spacing.lineRule
325-
if lineRule is None and spacing.line is not None:
326-
return WD_LINE_SPACING.MULTIPLE
327-
return lineRule
328-
329-
@spacing_lineRule.setter
330-
def spacing_lineRule(self, value):
331-
if value is None and self.spacing is None:
332-
return
333-
self.get_or_add_spacing().lineRule = value
334-
335-
@property
336-
def style(self):
337-
"""
338-
String contained in <w:pStyle> child, or None if that element is not
339-
present.
340-
"""
341-
pStyle = self.pStyle
342-
if pStyle is None:
343-
return None
344-
return pStyle.val
345-
346-
@style.setter
347-
def style(self, style):
348-
"""
349-
Set val attribute of <w:pStyle> child element to *style*, adding a
350-
new element if necessary. If *style* is |None|, remove the <w:pStyle>
351-
element if present.
352-
"""
353-
if style is None:
354-
self._remove_pStyle()
355-
return
356-
pStyle = self.get_or_add_pStyle()
357-
pStyle.val = style
358-
359-
@property
360-
def widowControl_val(self):
361-
"""
362-
The value of `widowControl/@val` or |None| if not present.
363-
"""
364-
widowControl = self.widowControl
365-
if widowControl is None:
366-
return None
367-
return widowControl.val
368-
369-
@widowControl_val.setter
370-
def widowControl_val(self, value):
371-
if value is None:
372-
self._remove_widowControl()
373-
else:
374-
self.get_or_add_widowControl().val = value
375-
376-
377-
class CT_Spacing(BaseOxmlElement):
378-
"""
379-
``<w:spacing>`` element, specifying paragraph spacing attributes such as
380-
space before and line spacing.
381-
"""
382-
after = OptionalAttribute('w:after', ST_TwipsMeasure)
383-
before = OptionalAttribute('w:before', ST_TwipsMeasure)
384-
line = OptionalAttribute('w:line', ST_SignedTwipsMeasure)
385-
lineRule = OptionalAttribute('w:lineRule', WD_LINE_SPACING)

0 commit comments

Comments
 (0)