Skip to content

Commit ee719a9

Browse files
author
Steve Canny
committed
parfmt: add ParagraphFormat.space_after getter
1 parent 4497b10 commit ee719a9

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

docx/oxml/text/paragraph.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,20 @@ def jc_val(self, value):
129129
return
130130
self.get_or_add_jc().val = value
131131

132+
@property
133+
def spacing_after(self):
134+
"""
135+
The value of `w:spacing/@w:after` or |None| if not present.
136+
"""
137+
spacing = self.spacing
138+
if spacing is None:
139+
return None
140+
return spacing.after
141+
132142
@property
133143
def spacing_before(self):
134144
"""
135-
The value of `w:spacing@w:before` or |None| if not present.
145+
The value of `w:spacing/@w:before` or |None| if not present.
136146
"""
137147
spacing = self.spacing
138148
if spacing is None:
@@ -169,4 +179,5 @@ class CT_Spacing(BaseOxmlElement):
169179
``<w:spacing>`` element, specifying paragraph spacing attributes such as
170180
space before and line spacing.
171181
"""
182+
after = OptionalAttribute('w:after', ST_TwipsMeasure)
172183
before = OptionalAttribute('w:before', ST_TwipsMeasure)

docx/text/paragraph.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ def alignment(self, value):
155155
pPr = self._element.get_or_add_pPr()
156156
pPr.jc_val = value
157157

158+
@property
159+
def space_after(self):
160+
"""
161+
|Length| value specifying the spacing to appear between this
162+
paragraph and the subsequent paragraph. |None| indicates this value
163+
is inherited from the style hierarchy. |Length| objects provide
164+
convenience properties, such as :attr:`~.Length.pt` and
165+
:attr:`~.Length.inches`, that allow easy conversion to various length
166+
units.
167+
"""
168+
pPr = self._element.pPr
169+
if pPr is None:
170+
return None
171+
return pPr.spacing_after
172+
158173
@property
159174
def space_before(self):
160175
"""

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Feature: Get or set paragraph formatting properties
2727
| right | None | None |
2828

2929

30-
@wip
3130
Scenario Outline: Get paragraph spacing
3231
Given a paragraph format having <setting> space <side>
3332
Then paragraph_format.space_<side> is <value>

tests/text/test_paragraph.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ def it_knows_its_space_before(self, space_before_get_fixture):
301301
paragraph_format, expected_value = space_before_get_fixture
302302
assert paragraph_format.space_before == expected_value
303303

304+
def it_knows_its_space_after(self, space_after_get_fixture):
305+
paragraph_format, expected_value = space_after_get_fixture
306+
assert paragraph_format.space_after == expected_value
307+
304308
# fixtures -------------------------------------------------------
305309

306310
@pytest.fixture(params=[
@@ -331,11 +335,22 @@ def alignment_set_fixture(self, request):
331335
expected_xml = xml(expected_cxml)
332336
return paragraph_format, value, expected_xml
333337

338+
@pytest.fixture(params=[
339+
('w:p', None),
340+
('w:p/w:pPr', None),
341+
('w:p/w:pPr/w:spacing', None),
342+
('w:p/w:pPr/w:spacing{w:after=240}', Pt(12)),
343+
])
344+
def space_after_get_fixture(self, request):
345+
p_cxml, expected_value = request.param
346+
paragraph_format = ParagraphFormat(element(p_cxml))
347+
return paragraph_format, expected_value
348+
334349
@pytest.fixture(params=[
335350
('w:p', None),
336351
('w:p/w:pPr', None),
337352
('w:p/w:pPr/w:spacing', None),
338-
('w:p/w:pPr/w:spacing{w:before=240}', Pt(12)),
353+
('w:p/w:pPr/w:spacing{w:before=420}', Pt(21)),
339354
])
340355
def space_before_get_fixture(self, request):
341356
p_cxml, expected_value = request.param

0 commit comments

Comments
 (0)