Skip to content

Commit 3664bed

Browse files
author
Steve Canny
committed
parfmt: add ParagraphFormat.line_spacing setter
1 parent 752f52f commit 3664bed

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

docx/oxml/text/paragraph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ def spacing_line(self):
171171
return None
172172
return spacing.line
173173

174+
@spacing_line.setter
175+
def spacing_line(self, value):
176+
if value is None and self.spacing is None:
177+
return
178+
self.get_or_add_spacing().line = value
179+
174180
@property
175181
def spacing_lineRule(self):
176182
"""
@@ -188,6 +194,12 @@ def spacing_lineRule(self):
188194
return WD_LINE_SPACING.MULTIPLE
189195
return lineRule
190196

197+
@spacing_lineRule.setter
198+
def spacing_lineRule(self, value):
199+
if value is None and self.spacing is None:
200+
return
201+
self.get_or_add_spacing().lineRule = value
202+
191203
@property
192204
def style(self):
193205
"""

docx/text/paragraph.py

Lines changed: 15 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, Twips
14+
from ..shared import ElementProxy, Emu, Length, Parented, Pt, Twips
1515

1616

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

178+
@line_spacing.setter
179+
def line_spacing(self, value):
180+
pPr = self._element.get_or_add_pPr()
181+
if value is None:
182+
pPr.spacing_line = None
183+
pPr.spacing_lineRule = None
184+
elif isinstance(value, Length):
185+
pPr.spacing_line = value
186+
if pPr.spacing_lineRule != WD_LINE_SPACING.AT_LEAST:
187+
pPr.spacing_lineRule = WD_LINE_SPACING.EXACTLY
188+
else:
189+
pPr.spacing_line = Emu(value * Twips(240))
190+
pPr.spacing_lineRule = WD_LINE_SPACING.MULTIPLE
191+
178192
@property
179193
def line_spacing_rule(self):
180194
"""

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Feature: Get or set paragraph formatting properties
6666
| double | 2.0 | WD_LINE_SPACING.DOUBLE |
6767

6868

69-
@wip
7069
Scenario Outline: Set line spacing
7170
Given a paragraph format having <setting> line spacing
7271
When I assign <new-value> to paragraph_format.line_spacing

tests/text/test_paragraph.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ 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_can_change_its_line_spacing(self, line_spacing_set_fixture):
323+
paragraph_format, value, expected_xml = line_spacing_set_fixture
324+
paragraph_format.line_spacing = value
325+
assert paragraph_format._element.xml == expected_xml
326+
322327
def it_knows_its_line_spacing_rule(self, line_spacing_rule_get_fixture):
323328
paragraph_format, expected_value = line_spacing_rule_get_fixture
324329
assert paragraph_format.line_spacing_rule == expected_value
@@ -366,6 +371,29 @@ def line_spacing_get_fixture(self, request):
366371
paragraph_format = ParagraphFormat(element(p_cxml))
367372
return paragraph_format, expected_value
368373

374+
@pytest.fixture(params=[
375+
('w:p', 1, 'w:p/w:pPr/w:spacing{w:line=240,w:lineRule=auto}'),
376+
('w:p', 2.0, 'w:p/w:pPr/w:spacing{w:line=480,w:lineRule=auto}'),
377+
('w:p', Pt(42), 'w:p/w:pPr/w:spacing{w:line=840,w:lineRule=exact}'),
378+
('w:p/w:pPr', 2,
379+
'w:p/w:pPr/w:spacing{w:line=480,w:lineRule=auto}'),
380+
('w:p/w:pPr/w:spacing{w:line=360}', 1,
381+
'w:p/w:pPr/w:spacing{w:line=240,w:lineRule=auto}'),
382+
('w:p/w:pPr/w:spacing{w:line=240,w:lineRule=exact}', 1.75,
383+
'w:p/w:pPr/w:spacing{w:line=420,w:lineRule=auto}'),
384+
('w:p/w:pPr/w:spacing{w:line=240,w:lineRule=atLeast}', Pt(42),
385+
'w:p/w:pPr/w:spacing{w:line=840,w:lineRule=atLeast}'),
386+
('w:p/w:pPr/w:spacing{w:line=240,w:lineRule=exact}', None,
387+
'w:p/w:pPr/w:spacing'),
388+
('w:p/w:pPr', None,
389+
'w:p/w:pPr'),
390+
])
391+
def line_spacing_set_fixture(self, request):
392+
p_cxml, value, expected_p_cxml = request.param
393+
paragraph_format = ParagraphFormat(element(p_cxml))
394+
expected_xml = xml(expected_p_cxml)
395+
return paragraph_format, value, expected_xml
396+
369397
@pytest.fixture(params=[
370398
('w:p', None),
371399
('w:p/w:pPr', None),

0 commit comments

Comments
 (0)