Skip to content

Commit 595cc87

Browse files
author
Steve Canny
committed
api: add Document.add_page_break()
1 parent ec057f3 commit 595cc87

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

docx/api.py

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

1111
import os
1212

13+
from docx.enum.text import WD_BREAK
1314
from docx.opc.constants import CONTENT_TYPE as CT
1415
from docx.package import Package
1516

@@ -45,6 +46,16 @@ def add_heading(self, text='', level=1):
4546
style = 'Title' if level == 0 else 'Heading%d' % level
4647
return self.add_paragraph(text, style)
4748

49+
def add_page_break(self):
50+
"""
51+
Return a paragraph newly added to the end of the document and
52+
containing only a page break.
53+
"""
54+
p = self._document_part.add_paragraph()
55+
r = p.add_run()
56+
r.add_break(WD_BREAK.PAGE)
57+
return p
58+
4859
def add_paragraph(self, text='', style=None):
4960
"""
5061
Return a paragraph newly added to the end of the document, populated

docx/oxml/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def clear(self, clear_str):
3131

3232
@property
3333
def type(self):
34-
self.get(qn('w:type'))
34+
return self.get(qn('w:type'))
3535

3636
@type.setter
3737
def type(self, type_str):

features/api-add-page-break.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Add a page break
33
As a programmer using the basic python-docx API
44
I need a method that adds a hard page break on its own paragraph
55

6-
@wip
76
Scenario: Add a hard page break paragraph
87
Given a document
98
When I add a page break to the document

features/steps/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def then_last_paragraph_contains_only_a_page_break(context):
9191
document = context.document
9292
p = document.paragraphs[-1]
9393
assert len(p.runs) == 1
94-
assert len(p.runs[0]) == 1
94+
assert len(p.runs[0]._r) == 1
9595
assert p.runs[0]._r[0].type == 'page'
9696

9797

tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from docx.api import Document
10+
from docx.enum.text import WD_BREAK
1011
from docx.opc.constants import CONTENT_TYPE as CT
1112
from docx.package import Package
1213
from docx.parts.document import DocumentPart, InlineShapes
@@ -71,6 +72,14 @@ def it_can_add_a_styled_paragraph(self, add_styled_paragraph_fixture):
7172
p = document.add_paragraph(style=style)
7273
assert p.style == style
7374

75+
def it_can_add_a_page_break(self, add_page_break_fixture):
76+
document, document_part_, p_, r_ = add_page_break_fixture
77+
p = document.add_page_break()
78+
document_part_.add_paragraph.assert_called_once_with()
79+
p_.add_run.assert_called_once_with()
80+
r_.add_break.assert_called_once_with(WD_BREAK.PAGE)
81+
assert p is p_
82+
7483
def it_can_add_a_picture(self, add_picture_fixture):
7584
(document, image_path, width, height, inline_shapes_, expected_width,
7685
expected_height, picture_) = add_picture_fixture
@@ -112,6 +121,10 @@ def add_heading_fixture(self, request, document, add_paragraph_, p_):
112121
def add_empty_paragraph_fixture(self, document, document_part_, p_):
113122
return document, document_part_, p_
114123

124+
@pytest.fixture
125+
def add_page_break_fixture(self, document, document_part_, p_, r_):
126+
return document, document_part_, p_, r_
127+
115128
@pytest.fixture
116129
def add_paragraph_(self, request, p_):
117130
return method_mock(

0 commit comments

Comments
 (0)