Skip to content

Commit a665ad1

Browse files
author
Steve Canny
committed
sect: add Document.add_section()
Also, reorder fixtures a bit in test_api.py.
1 parent 9351335 commit a665ad1

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docx/api.py

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

1111
import os
1212

13+
from docx.enum.section import WD_SECTION
1314
from docx.enum.text import WD_BREAK
1415
from docx.opc.constants import CONTENT_TYPE as CT, RELATIONSHIP_TYPE as RT
1516
from docx.package import Package
@@ -100,6 +101,13 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
100101

101102
return picture
102103

104+
def add_section(self, start_type=WD_SECTION.NEW_PAGE):
105+
"""
106+
Return a |Section| object representing a new section added at the end
107+
of the document.
108+
"""
109+
return self._document_part.add_section(start_type)
110+
103111
def add_table(self, rows, cols, style='LightShading-Accent1'):
104112
"""
105113
Add a table having row and column counts of *rows* and *cols*

docx/parts/document.py

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

1111
from collections import Sequence
1212

13+
from ..enum.section import WD_SECTION
1314
from ..opc.constants import RELATIONSHIP_TYPE as RT
1415
from ..opc.oxml import serialize_part_xml
1516
from ..opc.package import Part
@@ -37,6 +38,13 @@ def add_paragraph(self):
3738
"""
3839
return self.body.add_paragraph()
3940

41+
def add_section(self, start_type=WD_SECTION.NEW_PAGE):
42+
"""
43+
Return a |Section| object representing a new section added at the end
44+
of the document.
45+
"""
46+
raise NotImplementedError
47+
4048
def add_table(self, rows, cols):
4149
"""
4250
Return a table having *rows* rows and *cols* columns, newly appended

tests/test_api.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from docx.parts.document import DocumentPart, InlineShapes
1818
from docx.parts.numbering import NumberingPart
1919
from docx.parts.styles import StylesPart
20+
from docx.section import Section
2021
from docx.table import Table
2122
from docx.text import Paragraph, Run
2223

@@ -96,6 +97,14 @@ def it_can_add_a_picture(self, add_picture_fixture):
9697
assert picture.height == expected_height
9798
assert picture is picture_
9899

100+
def it_can_add_a_section(self, add_section_fixture):
101+
document, start_type_, section_ = add_section_fixture
102+
section = document.add_section(start_type_)
103+
document._document_part.add_section.assert_called_once_with(
104+
start_type_
105+
)
106+
assert section is section_
107+
99108
def it_can_add_a_table(self, add_table_fixture):
100109
document, rows, cols, style, document_part_, expected_style, table_ = (
101110
add_table_fixture
@@ -200,6 +209,10 @@ def add_picture_fixture(
200209
expected_width, expected_height, picture_
201210
)
202211

212+
@pytest.fixture
213+
def add_section_fixture(self, document, start_type_, section_):
214+
return document, start_type_, section_
215+
203216
@pytest.fixture
204217
def add_styled_paragraph_fixture(self, document, p_):
205218
style = 'foobaresque'
@@ -269,11 +282,13 @@ def document(self, open_):
269282
return Document()
270283

271284
@pytest.fixture
272-
def document_part_(self, request, p_, paragraphs_, table_, tables_):
285+
def document_part_(
286+
self, request, p_, paragraphs_, section_, table_, tables_):
273287
document_part_ = instance_mock(
274288
request, DocumentPart, content_type=CT.WML_DOCUMENT_MAIN
275289
)
276290
document_part_.add_paragraph.return_value = p_
291+
document_part_.add_section.return_value = section_
277292
document_part_.add_table.return_value = table_
278293
document_part_.paragraphs = paragraphs_
279294
document_part_.tables = tables_
@@ -336,6 +351,14 @@ def paragraphs_(self, request):
336351
def r_(self, request):
337352
return instance_mock(request, Run)
338353

354+
@pytest.fixture
355+
def section_(self, request):
356+
return instance_mock(request, Section)
357+
358+
@pytest.fixture
359+
def start_type_(self, request):
360+
return instance_mock(request, int)
361+
339362
@pytest.fixture
340363
def StylesPart_(self, request, styles_part_):
341364
StylesPart_ = class_mock(request, 'docx.api.StylesPart')

0 commit comments

Comments
 (0)