Skip to content

Commit 752f52f

Browse files
author
Steve Canny
committed
parfmt: add ParaFormat.line_spacing_rule getter
1 parent c38bf8e commit 752f52f

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

docx/text/paragraph.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..enum.style import WD_STYLE_TYPE
1212
from ..enum.text import WD_LINE_SPACING
1313
from .run import Run
14-
from ..shared import ElementProxy, Parented, Pt
14+
from ..shared import ElementProxy, Parented, Pt, Twips
1515

1616

1717
class Paragraph(Parented):
@@ -175,6 +175,22 @@ def line_spacing(self):
175175
return None
176176
return self._line_spacing(pPr.spacing_line, pPr.spacing_lineRule)
177177

178+
@property
179+
def line_spacing_rule(self):
180+
"""
181+
A member of the :ref:`WdLineSpacing` enumeration indicating how the
182+
value of :attr:`line_spacing` should be interpreted. Assigning any of
183+
the :ref:`WdLineSpacing` members :attr:`SINGLE`, :attr:`DOUBLE`, or
184+
:attr:`ONE_POINT_FIVE` will cause the value of :attr:`line_spacing`
185+
to be updated to produce the corresponding line spacing.
186+
"""
187+
pPr = self._element.pPr
188+
if pPr is None:
189+
return None
190+
return self._line_spacing_rule(
191+
pPr.spacing_line, pPr.spacing_lineRule
192+
)
193+
178194
@property
179195
def space_after(self):
180196
"""
@@ -227,3 +243,20 @@ def _line_spacing(spacing_line, spacing_lineRule):
227243
if spacing_lineRule == WD_LINE_SPACING.MULTIPLE:
228244
return spacing_line / Pt(12)
229245
return spacing_line
246+
247+
@staticmethod
248+
def _line_spacing_rule(line, lineRule):
249+
"""
250+
Return the line spacing rule value calculated from the combination of
251+
*line* and *lineRule*. Returns special members of the
252+
:ref:`WdLineSpacing` enumeration when line spacing is single, double,
253+
or 1.5 lines.
254+
"""
255+
if lineRule == WD_LINE_SPACING.MULTIPLE:
256+
if line == Twips(240):
257+
return WD_LINE_SPACING.SINGLE
258+
if line == Twips(360):
259+
return WD_LINE_SPACING.ONE_POINT_FIVE
260+
if line == Twips(480):
261+
return WD_LINE_SPACING.DOUBLE
262+
return lineRule

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Feature: Get or set paragraph formatting properties
5454
| after | 42 pt | None | None |
5555

5656

57-
@wip
5857
Scenario Outline: Get line spacing
5958
Given a paragraph format having <setting> line spacing
6059
Then paragraph_format.line_spacing is <value>

tests/text/test_paragraph.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from docx.enum.style import WD_STYLE_TYPE
12-
from docx.enum.text import WD_ALIGN_PARAGRAPH
12+
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_LINE_SPACING
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
@@ -319,6 +319,10 @@ def it_knows_its_line_spacing(self, line_spacing_get_fixture):
319319
paragraph_format, expected_value = line_spacing_get_fixture
320320
assert paragraph_format.line_spacing == expected_value
321321

322+
def it_knows_its_line_spacing_rule(self, line_spacing_rule_get_fixture):
323+
paragraph_format, expected_value = line_spacing_rule_get_fixture
324+
assert paragraph_format.line_spacing_rule == expected_value
325+
322326
# fixtures -------------------------------------------------------
323327

324328
@pytest.fixture(params=[
@@ -362,6 +366,26 @@ def line_spacing_get_fixture(self, request):
362366
paragraph_format = ParagraphFormat(element(p_cxml))
363367
return paragraph_format, expected_value
364368

369+
@pytest.fixture(params=[
370+
('w:p', None),
371+
('w:p/w:pPr', None),
372+
('w:p/w:pPr/w:spacing', None),
373+
('w:p/w:pPr/w:spacing{w:line=240}', WD_LINE_SPACING.SINGLE),
374+
('w:p/w:pPr/w:spacing{w:line=360}', WD_LINE_SPACING.ONE_POINT_FIVE),
375+
('w:p/w:pPr/w:spacing{w:line=480}', WD_LINE_SPACING.DOUBLE),
376+
('w:p/w:pPr/w:spacing{w:line=420}', WD_LINE_SPACING.MULTIPLE),
377+
('w:p/w:pPr/w:spacing{w:lineRule=auto}',
378+
WD_LINE_SPACING.MULTIPLE),
379+
('w:p/w:pPr/w:spacing{w:lineRule=exact}',
380+
WD_LINE_SPACING.EXACTLY),
381+
('w:p/w:pPr/w:spacing{w:lineRule=atLeast}',
382+
WD_LINE_SPACING.AT_LEAST),
383+
])
384+
def line_spacing_rule_get_fixture(self, request):
385+
p_cxml, expected_value = request.param
386+
paragraph_format = ParagraphFormat(element(p_cxml))
387+
return paragraph_format, expected_value
388+
365389
@pytest.fixture(params=[
366390
('w:p', None),
367391
('w:p/w:pPr', None),

0 commit comments

Comments
 (0)