Skip to content

Commit 118c438

Browse files
author
Steve Canny
committed
doc: add Document.sections
1 parent 64e1360 commit 118c438

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

docx/document.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .blkcntnr import BlockItemContainer
1212
from .enum.section import WD_SECTION
1313
from .enum.text import WD_BREAK
14-
from .section import Section
14+
from .section import Section, Sections
1515
from .shared import ElementProxy
1616

1717

@@ -139,6 +139,14 @@ def save(self, path_or_stream):
139139
"""
140140
self._part.save(path_or_stream)
141141

142+
@property
143+
def sections(self):
144+
"""
145+
A |Sections| object providing access to each section in this
146+
document.
147+
"""
148+
return Sections(self._element)
149+
142150
@property
143151
def _body(self):
144152
"""

tests/test_document.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from docx.enum.text import WD_BREAK
1616
from docx.opc.coreprops import CoreProperties
1717
from docx.parts.document import DocumentPart, InlineShapes
18+
from docx.section import Sections
1819
from docx.shape import InlineShape
1920
from docx.table import Table
2021
from docx.text.paragraph import Paragraph
@@ -99,6 +100,12 @@ def it_provides_access_to_its_paragraphs(self, paragraphs_fixture):
99100
paragraphs = document.paragraphs
100101
assert paragraphs is paragraphs_
101102

103+
def it_provides_access_to_its_sections(self, sections_fixture):
104+
document, Sections_, sections_ = sections_fixture
105+
sections = document.sections
106+
Sections_.assert_called_once_with(document._element)
107+
assert sections is sections_
108+
102109
def it_provides_access_to_the_document_part(self, part_fixture):
103110
document, part_ = part_fixture
104111
assert document.part is part_
@@ -212,6 +219,13 @@ def save_fixture(self, document_part_):
212219
file_ = 'foobar.docx'
213220
return document, file_
214221

222+
@pytest.fixture
223+
def sections_fixture(self, Sections_, sections_):
224+
document_elm = element('w:document')
225+
document = Document(document_elm, None)
226+
Sections_.return_value = sections_
227+
return document, Sections_, sections_
228+
215229
# fixture components ---------------------------------------------
216230

217231
@pytest.fixture
@@ -262,6 +276,14 @@ def run_(self, request):
262276
def Section_(self, request):
263277
return class_mock(request, 'docx.document.Section')
264278

279+
@pytest.fixture
280+
def Sections_(self, request):
281+
return class_mock(request, 'docx.document.Sections')
282+
283+
@pytest.fixture
284+
def sections_(self, request):
285+
return instance_mock(request, Sections)
286+
265287
@pytest.fixture
266288
def table_(self, request):
267289
return instance_mock(request, Table, style='UNASSIGNED')

0 commit comments

Comments
 (0)