Skip to content

Commit 1375dc9

Browse files
onlyjusSteve Canny
authored andcommitted
para: add Paragraph.alignment getter
1 parent d7f2bef commit 1375dc9

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
125125
register_element_cls('w:tr', CT_Row)
126126

127127
from docx.oxml.text import (
128-
CT_Br, CT_P, CT_PPr, CT_R, CT_RPr, CT_Text, CT_Underline
128+
CT_Br, CT_Jc, CT_P, CT_PPr, CT_R, CT_RPr, CT_Text, CT_Underline
129129
)
130130
register_element_cls('w:b', CT_OnOff)
131131
register_element_cls('w:bCs', CT_OnOff)
@@ -137,6 +137,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
137137
register_element_cls('w:i', CT_OnOff)
138138
register_element_cls('w:iCs', CT_OnOff)
139139
register_element_cls('w:imprint', CT_OnOff)
140+
register_element_cls('w:jc', CT_Jc)
140141
register_element_cls('w:noProof', CT_OnOff)
141142
register_element_cls('w:oMath', CT_OnOff)
142143
register_element_cls('w:outline', CT_OnOff)

docx/oxml/text.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
(CT_R).
66
"""
77

8-
from ..enum.text import WD_UNDERLINE
8+
from ..enum.text import WD_ALIGN_PARAGRAPH, WD_UNDERLINE
99
from .ns import qn
1010
from .simpletypes import ST_BrClear, ST_BrType
1111
from .xmlchemy import (
12-
BaseOxmlElement, OptionalAttribute, OxmlElement, ZeroOrMore, ZeroOrOne
12+
BaseOxmlElement, OptionalAttribute, OxmlElement, RequiredAttribute,
13+
ZeroOrMore, ZeroOrOne
1314
)
1415

1516

@@ -21,6 +22,13 @@ class CT_Br(BaseOxmlElement):
2122
clear = OptionalAttribute('w:clear', ST_BrClear)
2223

2324

25+
class CT_Jc(BaseOxmlElement):
26+
"""
27+
``<w:jc>`` element, specifying paragraph justification.
28+
"""
29+
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)
30+
31+
2432
class CT_P(BaseOxmlElement):
2533
"""
2634
``<w:p>`` element, containing the properties and text for a paragraph.
@@ -40,6 +48,17 @@ def add_p_before(self):
4048
self.addprevious(new_p)
4149
return new_p
4250

51+
@property
52+
def alignment(self):
53+
"""
54+
The value of the ``<w:jc>`` grandchild element or |None| if not
55+
present.
56+
"""
57+
pPr = self.pPr
58+
if pPr is None:
59+
return None
60+
return pPr.alignment
61+
4362
def clear_content(self):
4463
"""
4564
Remove all child elements, except the ``<w:pPr>`` element if present.
@@ -96,12 +115,23 @@ class CT_PPr(BaseOxmlElement):
96115
)
97116
pStyle = ZeroOrOne('w:pStyle')
98117
numPr = ZeroOrOne('w:numPr', successors=__child_sequence__[7:])
118+
jc = ZeroOrOne('w:jc', successors=__child_sequence__[27:])
99119
sectPr = ZeroOrOne('w:sectPr', successors=('w:pPrChange',))
100120

101121
def _insert_pStyle(self, pStyle):
102122
self.insert(0, pStyle)
103123
return pStyle
104124

125+
@property
126+
def alignment(self):
127+
"""
128+
The value of the ``<w:jc>`` child element or |None| if not present.
129+
"""
130+
jc = self.jc
131+
if jc is None:
132+
return None
133+
return jc.val
134+
105135
@property
106136
def style(self):
107137
"""

docx/text.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def add_run(self, text=None, style=None):
7575
run.style = style
7676
return run
7777

78+
@property
79+
def alignment(self):
80+
"""
81+
A member of the :ref:`WdParagraphAlignment` enumeration specifying
82+
the justification setting for this paragraph. A value of |None|
83+
indicates the paragraph has no directly-applied alignment value and
84+
will inherit its alignment value from its style hierarchy. Assigning
85+
|None| to this property removes any directly-applied alignment value.
86+
"""
87+
return self._p.alignment
88+
7889
def clear(self):
7990
"""
8091
Return this same paragraph after removing all its content.

tests/oxml/unitdata/text.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ def __init__(self, tag):
2323
super(CT_EmptyBuilder, self).__init__()
2424

2525

26+
class CT_JcBuilder(BaseBuilder):
27+
__tag__ = 'w:jc'
28+
__nspfxs__ = ('w',)
29+
__attrs__ = ('w:val',)
30+
31+
2632
class CT_PBuilder(BaseBuilder):
2733
__tag__ = 'w:p'
2834
__nspfxs__ = ('w',)
@@ -63,7 +69,7 @@ def with_space(self, value):
6369
return self
6470

6571

66-
class CT_Underline(BaseBuilder):
72+
class CT_UnderlineBuilder(BaseBuilder):
6773
__tag__ = 'w:u'
6874
__nspfxs__ = ('w',)
6975
__attrs__ = (
@@ -99,6 +105,10 @@ def a_dstrike():
99105
return CT_OnOffBuilder('w:dstrike')
100106

101107

108+
def a_jc():
109+
return CT_JcBuilder()
110+
111+
102112
def a_noProof():
103113
return CT_OnOffBuilder('w:noProof')
104114

@@ -156,7 +166,7 @@ def a_t():
156166

157167

158168
def a_u():
159-
return CT_Underline()
169+
return CT_UnderlineBuilder()
160170

161171

162172
def an_emboss():

tests/test_text.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from docx.enum.text import WD_BREAK, WD_UNDERLINE
11+
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE
1212
from docx.oxml import OxmlElement
1313
from docx.oxml.ns import qn
1414
from docx.oxml.text import CT_P, CT_R
@@ -18,10 +18,11 @@
1818

1919
from .oxml.parts.unitdata.document import a_body
2020
from .oxml.unitdata.text import (
21-
a_b, a_bCs, a_br, a_caps, a_cr, a_cs, a_dstrike, a_p, a_pPr, a_pStyle,
22-
a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike, a_t, a_tab,
23-
a_u, a_vanish, a_webHidden, an_emboss, an_i, an_iCs, an_imprint,
24-
an_oMath, a_noProof, an_outline, an_r, an_rPr, an_rStyle, an_rtl
21+
a_b, a_bCs, a_br, a_caps, a_cr, a_cs, a_dstrike, a_jc, a_p, a_pPr,
22+
a_pStyle, a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike,
23+
a_t, a_tab, a_u, a_vanish, a_webHidden, an_emboss, an_i, an_iCs,
24+
an_imprint, an_oMath, a_noProof, an_outline, an_r, an_rPr, an_rStyle,
25+
an_rtl
2526
)
2627
from .unitutil import call, class_mock, instance_mock, Mock
2728

@@ -41,6 +42,10 @@ def it_can_add_a_run_to_itself(self, add_run_fixture):
4142
assert isinstance(run, Run)
4243
assert run._r is paragraph._p.r_lst[0]
4344

45+
def it_knows_its_alignment_value(self, alignment_get_fixture):
46+
paragraph, expected_value = alignment_get_fixture
47+
assert paragraph.alignment == expected_value
48+
4449
def it_knows_its_paragraph_style(self):
4550
cases = (
4651
(Mock(name='p_elm', style='foobar'), 'foobar'),
@@ -102,6 +107,19 @@ def add_run_fixture(self, request, paragraph):
102107
expected_xml = a_p().with_nsdecls().with_child(r_bldr).xml()
103108
return paragraph, text, style, expected_xml
104109

110+
@pytest.fixture(params=[
111+
('center', WD_ALIGN_PARAGRAPH.CENTER),
112+
(None, None),
113+
])
114+
def alignment_get_fixture(self, request):
115+
jc_val, expected_alignment_value = request.param
116+
p_bldr = a_p().with_nsdecls()
117+
if jc_val is not None:
118+
p_bldr.with_child(a_pPr().with_child(a_jc().with_val(jc_val)))
119+
p = p_bldr.element
120+
paragraph = Paragraph(p)
121+
return paragraph, expected_alignment_value
122+
105123
@pytest.fixture
106124
def clear_fixture(self, request):
107125
"""

0 commit comments

Comments
 (0)