Skip to content

Commit f4e6257

Browse files
author
Steve Canny
committed
sect: add DocumentPart.add_section()
Also, reorder fixtures in test_document.py
1 parent 56c31be commit f4e6257

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

docx/oxml/parts/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class CT_Body(BaseOxmlElement):
3333
p = ZeroOrMore('w:p', successors=('w:sectPr',))
3434
tbl = ZeroOrMore('w:tbl', successors=('w:sectPr',))
3535

36+
def add_section_break(self):
37+
"""
38+
Return the current ``<w:sectPr>`` element after adding a clone of it
39+
in a new ``<w:p>`` element appended to the block content elements.
40+
Note that the "current" ``<w:sectPr>`` will always be the sentinel
41+
sectPr in this case since we're always working at the end of the
42+
block content.
43+
"""
44+
raise NotImplementedError
45+
3646
def _insert_p(self, p):
3747
return self._append_blocklevelelt(p)
3848

docx/parts/document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def add_section(self, start_type=WD_SECTION.NEW_PAGE):
4343
Return a |Section| object representing a new section added at the end
4444
of the document.
4545
"""
46-
raise NotImplementedError
46+
new_sectPr = self._element.body.add_section_break()
47+
new_sectPr.start_type = start_type
48+
return Section(new_sectPr)
4749

4850
def add_table(self, rows, cols):
4951
"""

tests/parts/test_document.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from docx.opc.package import PartFactory
1515
from docx.opc.packuri import PackURI
1616
from docx.oxml.parts.document import CT_Body, CT_Document
17+
from docx.oxml.section import CT_SectPr
1718
from docx.oxml.text import CT_R
1819
from docx.package import ImageParts, Package
1920
from docx.parts.document import _Body, DocumentPart, InlineShapes, Sections
@@ -76,6 +77,15 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
7677
body_.add_paragraph.assert_called_once_with()
7778
assert p is p_
7879

80+
def it_can_add_a_section(self, add_section_fixture):
81+
(document_part, start_type_, body_elm_, new_sectPr_, Section_,
82+
section_) = add_section_fixture
83+
section = document_part.add_section(start_type_)
84+
body_elm_.add_section_break.assert_called_once_with()
85+
assert new_sectPr_.start_type == start_type_
86+
Section_.assert_called_once_with(new_sectPr_)
87+
assert section is section_
88+
7989
def it_can_add_a_table(self, add_table_fixture):
8090
document_part, rows, cols, body_, table_ = add_table_fixture
8191
table = document_part.add_table(rows, cols)
@@ -146,6 +156,16 @@ def add_paragraph_fixture(self, document_part_body_, body_, p_):
146156
document_part = DocumentPart(None, None, None, None)
147157
return document_part, body_, p_
148158

159+
@pytest.fixture
160+
def add_section_fixture(
161+
self, document_elm_, start_type_, body_elm_, sectPr_, Section_,
162+
section_):
163+
document_part = DocumentPart(None, None, document_elm_, None)
164+
return (
165+
document_part, start_type_, body_elm_, sectPr_, Section_,
166+
section_
167+
)
168+
149169
@pytest.fixture
150170
def add_table_fixture(self, document_part_body_, body_, table_):
151171
document_part = DocumentPart(None, None, None, None)
@@ -223,6 +243,12 @@ def body_(self, request, p_, table_):
223243
body_.add_table.return_value = table_
224244
return body_
225245

246+
@pytest.fixture
247+
def body_elm_(self, request, sectPr_):
248+
body_elm_ = instance_mock(request, CT_Body)
249+
body_elm_.add_section_break.return_value = sectPr_
250+
return body_elm_
251+
226252
@pytest.fixture
227253
def blob_(self, request):
228254
return instance_mock(request, str)
@@ -235,6 +261,10 @@ def content_type_(self, request):
235261
def document(self):
236262
return DocumentPart(None, None, None, None)
237263

264+
@pytest.fixture
265+
def document_elm_(self, request, body_elm_):
266+
return instance_mock(request, CT_Document, body=body_elm_)
267+
238268
@pytest.fixture
239269
def document_part_(self, request):
240270
return instance_mock(request, DocumentPart)
@@ -321,16 +351,34 @@ def relate_to_(self, request, rId_):
321351
def rId_(self, request):
322352
return instance_mock(request, str)
323353

354+
@pytest.fixture
355+
def Section_(self, request, section_):
356+
return class_mock(
357+
request, 'docx.parts.document.Section', return_value=section_
358+
)
359+
360+
@pytest.fixture
361+
def section_(self, request):
362+
return instance_mock(request, Section)
363+
324364
@pytest.fixture
325365
def Sections_(self, request):
326366
return class_mock(request, 'docx.parts.document.Sections')
327367

368+
@pytest.fixture
369+
def sectPr_(self, request):
370+
return instance_mock(request, CT_SectPr)
371+
328372
@pytest.fixture
329373
def serialize_part_xml_(self, request):
330374
return function_mock(
331375
request, 'docx.parts.document.serialize_part_xml'
332376
)
333377

378+
@pytest.fixture
379+
def start_type_(self, request):
380+
return instance_mock(request, int)
381+
334382
@pytest.fixture
335383
def table_(self, request):
336384
return instance_mock(request, Table)

0 commit comments

Comments
 (0)