Skip to content

Commit 8ed7151

Browse files
author
Steve Canny
committed
parfmt: add ParagraphFormat.keep_with_next getter
1 parent fcca0c1 commit 8ed7151

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
140140
register_element_cls('w:ind', CT_Ind)
141141
register_element_cls('w:jc', CT_Jc)
142142
register_element_cls('w:keepLines', CT_OnOff)
143+
register_element_cls('w:keepNext', CT_OnOff)
143144
register_element_cls('w:p', CT_P)
144145
register_element_cls('w:pPr', CT_PPr)
145146
register_element_cls('w:pStyle', CT_String)

docx/oxml/text/paragraph.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class CT_PPr(BaseOxmlElement):
117117
'w:rPr', 'w:sectPr', 'w:pPrChange'
118118
)
119119
pStyle = ZeroOrOne('w:pStyle', successors=_tag_seq[1:])
120+
keepNext = ZeroOrOne('w:keepNext', successors=_tag_seq[2:])
120121
keepLines = ZeroOrOne('w:keepLines', successors=_tag_seq[3:])
121122
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
122123
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
@@ -217,6 +218,16 @@ def keepLines_val(self):
217218
return None
218219
return keepLines.val
219220

221+
@property
222+
def keepNext_val(self):
223+
"""
224+
The value of `keepNext/@val` or |None| if not present.
225+
"""
226+
keepNext = self.keepNext
227+
if keepNext is None:
228+
return None
229+
return keepNext.val
230+
220231
@property
221232
def spacing_after(self):
222233
"""

docx/text/paragraph.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ def keep_together(self):
189189
return None
190190
return pPr.keepLines_val
191191

192+
@property
193+
def keep_with_next(self):
194+
"""
195+
|True| if the paragraph should be kept on the same page as the
196+
subsequent paragraph when the document is rendered. For example, this
197+
property could be used to keep a section heading on the same page as
198+
its first paragraph. |None| indicates its effective value is
199+
inherited from the style hierarchy.
200+
"""
201+
pPr = self._element.pPr
202+
if pPr is None:
203+
return None
204+
return pPr.keepNext_val
205+
192206
@property
193207
def left_indent(self):
194208
"""

tests/text/test_paragraph.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ def line_spacing_rule_set_fixture(self, request):
531531
('w:p', 'keep_together', None),
532532
('w:p/w:pPr/w:keepLines{w:val=on}', 'keep_together', True),
533533
('w:p/w:pPr/w:keepLines{w:val=0}', 'keep_together', False),
534+
('w:p', 'keep_with_next', None),
535+
('w:p/w:pPr/w:keepNext{w:val=1}', 'keep_with_next', True),
536+
('w:p/w:pPr/w:keepNext{w:val=false}', 'keep_with_next', False),
534537
])
535538
def on_off_get_fixture(self, request):
536539
p_cxml, prop_name, expected_value = request.param

0 commit comments

Comments
 (0)