Skip to content

Commit 86741a1

Browse files
author
Steve Canny
committed
parfmt: add ParagraphFormat.widow_control getter
1 parent 8f836d4 commit 86741a1

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
146146
register_element_cls('w:pPr', CT_PPr)
147147
register_element_cls('w:pStyle', CT_String)
148148
register_element_cls('w:spacing', CT_Spacing)
149+
register_element_cls('w:widowControl', CT_OnOff)
149150

150151
from .text.run import (
151152
CT_Br, CT_Fonts, CT_HpsMeasure, CT_R, CT_RPr, CT_Text, CT_Underline,

docx/oxml/text/paragraph.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class CT_PPr(BaseOxmlElement):
120120
keepNext = ZeroOrOne('w:keepNext', successors=_tag_seq[2:])
121121
keepLines = ZeroOrOne('w:keepLines', successors=_tag_seq[3:])
122122
pageBreakBefore = ZeroOrOne('w:pageBreakBefore', successors=_tag_seq[4:])
123+
widowControl = ZeroOrOne('w:widowControl', successors=_tag_seq[6:])
123124
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
124125
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
125126
ind = ZeroOrOne('w:ind', successors=_tag_seq[23:])
@@ -334,6 +335,16 @@ def style(self, style):
334335
pStyle = self.get_or_add_pStyle()
335336
pStyle.val = style
336337

338+
@property
339+
def widowControl_val(self):
340+
"""
341+
The value of `widowControl/@val` or |None| if not present.
342+
"""
343+
widowControl = self.widowControl
344+
if widowControl is None:
345+
return None
346+
return widowControl.val
347+
337348

338349
class CT_Spacing(BaseOxmlElement):
339350
"""

docx/text/paragraph.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ def space_before(self):
351351
def space_before(self, value):
352352
self._element.get_or_add_pPr().spacing_before = value
353353

354+
@property
355+
def widow_control(self):
356+
"""
357+
|True| if the first and last lines in the paragraph remain on the
358+
same page as the rest of the paragraph when Word repaginates the
359+
document. |None| indicates its effective value is inherited from the
360+
style hierarchy.
361+
"""
362+
pPr = self._element.pPr
363+
if pPr is None:
364+
return None
365+
return pPr.widowControl_val
366+
354367
@staticmethod
355368
def _line_spacing(spacing_line, spacing_lineRule):
356369
"""

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ Feature: Get or set paragraph formatting properties
128128
| right | 17.3 pt | None | None |
129129

130130

131-
@wip
132131
Scenario Outline: Get On/Off paragraph property
133132
Given a paragraph format having <prop-name> set <state>
134133
Then paragraph_format.<prop-name> is <value>

tests/text/test_paragraph.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ def line_spacing_rule_set_fixture(self, request):
537537
('w:p', 'page_break_before', None),
538538
('w:p/w:pPr/w:pageBreakBefore', 'page_break_before', True),
539539
('w:p/w:pPr/w:pageBreakBefore{w:val=0}', 'page_break_before', False),
540+
('w:p', 'widow_control', None),
541+
('w:p/w:pPr/w:widowControl{w:val=true}', 'widow_control', True),
542+
('w:p/w:pPr/w:widowControl{w:val=off}', 'widow_control', False),
540543
])
541544
def on_off_get_fixture(self, request):
542545
p_cxml, prop_name, expected_value = request.param

0 commit comments

Comments
 (0)