Skip to content

Commit aaae2e6

Browse files
author
Steve Canny
committed
doc: add Document.add_page_break()
1 parent 57d67eb commit aaae2e6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

docx/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from .blkcntnr import BlockItemContainer
12+
from .enum.text import WD_BREAK
1213
from .shared import ElementProxy
1314

1415

@@ -38,6 +39,15 @@ def add_heading(self, text='', level=1):
3839
style = 'Title' if level == 0 else 'Heading %d' % level
3940
return self.add_paragraph(text, style)
4041

42+
def add_page_break(self):
43+
"""
44+
Return a paragraph newly added to the end of the document and
45+
containing only a page break.
46+
"""
47+
paragraph = self.add_paragraph()
48+
paragraph.add_run().add_break(WD_BREAK.PAGE)
49+
return paragraph
50+
4151
def add_paragraph(self, text='', style=None):
4252
"""
4353
Return a paragraph newly added to the end of the document, populated

tests/test_document.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import pytest
1212

1313
from docx.document import _Body, Document
14+
from docx.enum.text import WD_BREAK
1415
from docx.parts.document import DocumentPart
1516
from docx.text.paragraph import Paragraph
17+
from docx.text.run import Run
1618

1719
from .unitutil.cxml import element
1820
from .unitutil.mock import (
@@ -35,6 +37,14 @@ def it_raises_on_heading_level_out_of_range(self):
3537
with pytest.raises(ValueError):
3638
document.add_heading(level=10)
3739

40+
def it_can_add_a_page_break(self, add_page_break_fixture):
41+
document, paragraph_, run_ = add_page_break_fixture
42+
paragraph = document.add_page_break()
43+
document.add_paragraph.assert_called_once_with()
44+
paragraph_.add_run.assert_called_once_with()
45+
run_.add_break.assert_called_once_with(WD_BREAK.PAGE)
46+
assert paragraph is paragraph_
47+
3848
def it_can_add_a_paragraph(self, add_paragraph_fixture):
3949
document, text, style, paragraph_ = add_paragraph_fixture
4050
paragraph = document.add_paragraph(text, style)
@@ -66,6 +76,13 @@ def add_heading_fixture(self, request, add_paragraph_, paragraph_):
6676
add_paragraph_.return_value = paragraph_
6777
return document, text, level, style, paragraph_
6878

79+
@pytest.fixture
80+
def add_page_break_fixture(self, add_paragraph_, paragraph_, run_):
81+
document = Document(None, None)
82+
add_paragraph_.return_value = paragraph_
83+
paragraph_.add_run.return_value = run_
84+
return document, paragraph_, run_
85+
6986
@pytest.fixture(params=[
7087
('', None),
7188
('', 'Heading 1'),
@@ -114,3 +131,7 @@ def document_part_(self, request):
114131
@pytest.fixture
115132
def paragraph_(self, request):
116133
return instance_mock(request, Paragraph)
134+
135+
@pytest.fixture
136+
def run_(self, request):
137+
return instance_mock(request, Run)

0 commit comments

Comments
 (0)