Skip to content

Commit a076932

Browse files
author
Steve Canny
committed
parfmt: add ParagraphFormat.alignment getter
* rename CT_PPr.alignment => .jc_val
1 parent 2be2c92 commit a076932

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

docx/oxml/text/paragraph.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def alignment(self):
4646
pPr = self.pPr
4747
if pPr is None:
4848
return None
49-
return pPr.alignment
49+
return pPr.jc_val
5050

5151
@alignment.setter
5252
def alignment(self, value):
5353
pPr = self.get_or_add_pPr()
54-
pPr.alignment = value
54+
pPr.jc_val = value
5555

5656
def clear_content(self):
5757
"""
@@ -113,7 +113,7 @@ def _insert_pStyle(self, pStyle):
113113
return pStyle
114114

115115
@property
116-
def alignment(self):
116+
def jc_val(self):
117117
"""
118118
The value of the ``<w:jc>`` child element or |None| if not present.
119119
"""
@@ -122,13 +122,12 @@ def alignment(self):
122122
return None
123123
return jc.val
124124

125-
@alignment.setter
126-
def alignment(self, value):
125+
@jc_val.setter
126+
def jc_val(self, value):
127127
if value is None:
128128
self._remove_jc()
129129
return
130-
jc = self.get_or_add_jc()
131-
jc.val = value
130+
self.get_or_add_jc().val = value
132131

133132
@property
134133
def style(self):

docx/text/paragraph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,15 @@ class ParagraphFormat(ElementProxy):
137137
"""
138138

139139
__slots__ = ()
140+
141+
@property
142+
def alignment(self):
143+
"""
144+
A member of the :ref:`WdParagraphAlignment` enumeration specifying
145+
the justification setting for this paragraph. A value of |None|
146+
indicates paragraph alignment is inherited from the style hierarchy.
147+
"""
148+
pPr = self._element.pPr
149+
if pPr is None:
150+
return None
151+
return pPr.jc_val

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Get or set paragraph formatting properties
44
I need a ParagraphFormat object with read/write formatting properties
55

66

7-
@wip
87
Scenario Outline: Get paragraph alignment
98
Given a paragraph format having <align-type> alignment
109
Then paragraph_format.alignment is <value>

tests/text/test_paragraph.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from docx.oxml.text.paragraph import CT_P
1414
from docx.oxml.text.run import CT_R
1515
from docx.parts.document import DocumentPart
16-
from docx.text.paragraph import Paragraph
16+
from docx.text.paragraph import Paragraph, ParagraphFormat
1717
from docx.text.run import Run
1818

1919
import pytest
@@ -283,3 +283,22 @@ def runs_(self, request):
283283
run_ = instance_mock(request, Run, name='run_')
284284
run_2_ = instance_mock(request, Run, name='run_2_')
285285
return run_, run_2_
286+
287+
288+
class DescribeParagraphFormat(object):
289+
290+
def it_knows_its_alignment_value(self, alignment_get_fixture):
291+
paragraph_format, expected_value = alignment_get_fixture
292+
assert paragraph_format.alignment == expected_value
293+
294+
# fixtures -------------------------------------------------------
295+
296+
@pytest.fixture(params=[
297+
('w:p', None),
298+
('w:p/w:pPr', None),
299+
('w:p/w:pPr/w:jc{w:val=center}', WD_ALIGN_PARAGRAPH.CENTER),
300+
])
301+
def alignment_get_fixture(self, request):
302+
p_cxml, expected_value = request.param
303+
paragraph_format = ParagraphFormat(element(p_cxml))
304+
return paragraph_format, expected_value

0 commit comments

Comments
 (0)