Skip to content

Commit 5b924f4

Browse files
author
Steve Canny
committed
reorg: extract docx.oxml.text.paragraph module
1 parent afc60c1 commit 5b924f4

File tree

4 files changed

+157
-158
lines changed

4 files changed

+157
-158
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
134134
register_element_cls('w:tr', CT_Row)
135135
register_element_cls('w:vMerge', CT_VMerge)
136136

137-
from .text import CT_Jc, CT_P, CT_PPr
137+
from .text.paragraph import CT_Jc, CT_P, CT_PPr
138138
register_element_cls('w:jc', CT_Jc)
139139
register_element_cls('w:p', CT_P)
140140
register_element_cls('w:pPr', CT_PPr)

docx/oxml/text/__init__.py

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +0,0 @@
1-
# encoding: utf-8
2-
3-
"""
4-
Custom element classes related to text, such as paragraph (CT_P) and runs
5-
(CT_R).
6-
"""
7-
8-
from ...enum.text import WD_ALIGN_PARAGRAPH
9-
from ..ns import qn
10-
from ..xmlchemy import (
11-
BaseOxmlElement, OxmlElement, RequiredAttribute, ZeroOrMore, ZeroOrOne
12-
)
13-
14-
15-
class CT_Jc(BaseOxmlElement):
16-
"""
17-
``<w:jc>`` element, specifying paragraph justification.
18-
"""
19-
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)
20-
21-
22-
class CT_P(BaseOxmlElement):
23-
"""
24-
``<w:p>`` element, containing the properties and text for a paragraph.
25-
"""
26-
pPr = ZeroOrOne('w:pPr')
27-
r = ZeroOrMore('w:r')
28-
29-
def _insert_pPr(self, pPr):
30-
self.insert(0, pPr)
31-
return pPr
32-
33-
def add_p_before(self):
34-
"""
35-
Return a new ``<w:p>`` element inserted directly prior to this one.
36-
"""
37-
new_p = OxmlElement('w:p')
38-
self.addprevious(new_p)
39-
return new_p
40-
41-
@property
42-
def alignment(self):
43-
"""
44-
The value of the ``<w:jc>`` grandchild element or |None| if not
45-
present.
46-
"""
47-
pPr = self.pPr
48-
if pPr is None:
49-
return None
50-
return pPr.alignment
51-
52-
@alignment.setter
53-
def alignment(self, value):
54-
pPr = self.get_or_add_pPr()
55-
pPr.alignment = value
56-
57-
def clear_content(self):
58-
"""
59-
Remove all child elements, except the ``<w:pPr>`` element if present.
60-
"""
61-
for child in self[:]:
62-
if child.tag == qn('w:pPr'):
63-
continue
64-
self.remove(child)
65-
66-
def set_sectPr(self, sectPr):
67-
"""
68-
Unconditionally replace or add *sectPr* as a grandchild in the
69-
correct sequence.
70-
"""
71-
pPr = self.get_or_add_pPr()
72-
pPr._remove_sectPr()
73-
pPr._insert_sectPr(sectPr)
74-
75-
@property
76-
def style(self):
77-
"""
78-
String contained in w:val attribute of ./w:pPr/w:pStyle grandchild,
79-
or |None| if not present.
80-
"""
81-
pPr = self.pPr
82-
if pPr is None:
83-
return None
84-
return pPr.style
85-
86-
@style.setter
87-
def style(self, style):
88-
pPr = self.get_or_add_pPr()
89-
pPr.style = style
90-
91-
92-
class CT_PPr(BaseOxmlElement):
93-
"""
94-
``<w:pPr>`` element, containing the properties for a paragraph.
95-
"""
96-
__child_sequence__ = (
97-
'w:pStyle', 'w:keepNext', 'w:keepLines', 'w:pageBreakBefore',
98-
'w:framePr', 'w:widowControl', 'w:numPr', 'w:suppressLineNumbers',
99-
'w:pBdr', 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
100-
'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
101-
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
102-
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
103-
'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
104-
'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
105-
'w:rPr', 'w:sectPr', 'w:pPrChange'
106-
)
107-
pStyle = ZeroOrOne('w:pStyle')
108-
numPr = ZeroOrOne('w:numPr', successors=__child_sequence__[7:])
109-
jc = ZeroOrOne('w:jc', successors=__child_sequence__[27:])
110-
sectPr = ZeroOrOne('w:sectPr', successors=('w:pPrChange',))
111-
112-
def _insert_pStyle(self, pStyle):
113-
self.insert(0, pStyle)
114-
return pStyle
115-
116-
@property
117-
def alignment(self):
118-
"""
119-
The value of the ``<w:jc>`` child element or |None| if not present.
120-
"""
121-
jc = self.jc
122-
if jc is None:
123-
return None
124-
return jc.val
125-
126-
@alignment.setter
127-
def alignment(self, value):
128-
if value is None:
129-
self._remove_jc()
130-
return
131-
jc = self.get_or_add_jc()
132-
jc.val = value
133-
134-
@property
135-
def style(self):
136-
"""
137-
String contained in <w:pStyle> child, or None if that element is not
138-
present.
139-
"""
140-
pStyle = self.pStyle
141-
if pStyle is None:
142-
return None
143-
return pStyle.val
144-
145-
@style.setter
146-
def style(self, style):
147-
"""
148-
Set val attribute of <w:pStyle> child element to *style*, adding a
149-
new element if necessary. If *style* is |None|, remove the <w:pStyle>
150-
element if present.
151-
"""
152-
if style is None:
153-
self._remove_pStyle()
154-
return
155-
pStyle = self.get_or_add_pStyle()
156-
pStyle.val = style

docx/oxml/text/paragraph.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Custom element classes related to paragraphs (CT_P).
5+
"""
6+
7+
from ...enum.text import WD_ALIGN_PARAGRAPH
8+
from ..ns import qn
9+
from ..xmlchemy import (
10+
BaseOxmlElement, OxmlElement, RequiredAttribute, ZeroOrMore, ZeroOrOne
11+
)
12+
13+
14+
class CT_Jc(BaseOxmlElement):
15+
"""
16+
``<w:jc>`` element, specifying paragraph justification.
17+
"""
18+
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)
19+
20+
21+
class CT_P(BaseOxmlElement):
22+
"""
23+
``<w:p>`` element, containing the properties and text for a paragraph.
24+
"""
25+
pPr = ZeroOrOne('w:pPr')
26+
r = ZeroOrMore('w:r')
27+
28+
def _insert_pPr(self, pPr):
29+
self.insert(0, pPr)
30+
return pPr
31+
32+
def add_p_before(self):
33+
"""
34+
Return a new ``<w:p>`` element inserted directly prior to this one.
35+
"""
36+
new_p = OxmlElement('w:p')
37+
self.addprevious(new_p)
38+
return new_p
39+
40+
@property
41+
def alignment(self):
42+
"""
43+
The value of the ``<w:jc>`` grandchild element or |None| if not
44+
present.
45+
"""
46+
pPr = self.pPr
47+
if pPr is None:
48+
return None
49+
return pPr.alignment
50+
51+
@alignment.setter
52+
def alignment(self, value):
53+
pPr = self.get_or_add_pPr()
54+
pPr.alignment = value
55+
56+
def clear_content(self):
57+
"""
58+
Remove all child elements, except the ``<w:pPr>`` element if present.
59+
"""
60+
for child in self[:]:
61+
if child.tag == qn('w:pPr'):
62+
continue
63+
self.remove(child)
64+
65+
def set_sectPr(self, sectPr):
66+
"""
67+
Unconditionally replace or add *sectPr* as a grandchild in the
68+
correct sequence.
69+
"""
70+
pPr = self.get_or_add_pPr()
71+
pPr._remove_sectPr()
72+
pPr._insert_sectPr(sectPr)
73+
74+
@property
75+
def style(self):
76+
"""
77+
String contained in w:val attribute of ./w:pPr/w:pStyle grandchild,
78+
or |None| if not present.
79+
"""
80+
pPr = self.pPr
81+
if pPr is None:
82+
return None
83+
return pPr.style
84+
85+
@style.setter
86+
def style(self, style):
87+
pPr = self.get_or_add_pPr()
88+
pPr.style = style
89+
90+
91+
class CT_PPr(BaseOxmlElement):
92+
"""
93+
``<w:pPr>`` element, containing the properties for a paragraph.
94+
"""
95+
__child_sequence__ = (
96+
'w:pStyle', 'w:keepNext', 'w:keepLines', 'w:pageBreakBefore',
97+
'w:framePr', 'w:widowControl', 'w:numPr', 'w:suppressLineNumbers',
98+
'w:pBdr', 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
99+
'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
100+
'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
101+
'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
102+
'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
103+
'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
104+
'w:rPr', 'w:sectPr', 'w:pPrChange'
105+
)
106+
pStyle = ZeroOrOne('w:pStyle')
107+
numPr = ZeroOrOne('w:numPr', successors=__child_sequence__[7:])
108+
jc = ZeroOrOne('w:jc', successors=__child_sequence__[27:])
109+
sectPr = ZeroOrOne('w:sectPr', successors=('w:pPrChange',))
110+
111+
def _insert_pStyle(self, pStyle):
112+
self.insert(0, pStyle)
113+
return pStyle
114+
115+
@property
116+
def alignment(self):
117+
"""
118+
The value of the ``<w:jc>`` child element or |None| if not present.
119+
"""
120+
jc = self.jc
121+
if jc is None:
122+
return None
123+
return jc.val
124+
125+
@alignment.setter
126+
def alignment(self, value):
127+
if value is None:
128+
self._remove_jc()
129+
return
130+
jc = self.get_or_add_jc()
131+
jc.val = value
132+
133+
@property
134+
def style(self):
135+
"""
136+
String contained in <w:pStyle> child, or None if that element is not
137+
present.
138+
"""
139+
pStyle = self.pStyle
140+
if pStyle is None:
141+
return None
142+
return pStyle.val
143+
144+
@style.setter
145+
def style(self, style):
146+
"""
147+
Set val attribute of <w:pStyle> child element to *style*, adding a
148+
new element if necessary. If *style* is |None|, remove the <w:pStyle>
149+
element if present.
150+
"""
151+
if style is None:
152+
self._remove_pStyle()
153+
return
154+
pStyle = self.get_or_add_pStyle()
155+
pStyle.val = style

tests/test_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE
1212
from docx.oxml.ns import qn
13-
from docx.oxml.text import CT_P
13+
from docx.oxml.text.paragraph import CT_P
1414
from docx.oxml.text.run import CT_R
1515
from docx.parts.document import InlineShapes
1616
from docx.shape import InlineShape

0 commit comments

Comments
 (0)