Skip to content

Commit c161243

Browse files
author
Steve Canny
committed
sect: add Document.sections
and DocumentPart.sections
1 parent 6b0014a commit c161243

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

docx/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ def save(self, path_or_stream):
147147
"""
148148
self._package.save(path_or_stream)
149149

150+
@property
151+
def sections(self):
152+
"""
153+
Return a reference to the |Sections| instance for this document.
154+
"""
155+
return self._document_part.sections
156+
150157
@lazyproperty
151158
def styles_part(self):
152159
"""

docx/parts/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ def part(self):
110110
"""
111111
return self
112112

113+
@lazyproperty
114+
def sections(self):
115+
"""
116+
The |Sections| instance organizing the sections in this document.
117+
"""
118+
return Sections(self._element)
119+
113120
@property
114121
def tables(self):
115122
"""
@@ -221,3 +228,6 @@ class Sections(object):
221228
Sequence of |Section| objects corresponding to the sections in the
222229
document.
223230
"""
231+
def __init__(self, document_elm):
232+
super(Sections, self).__init__()
233+
self._document_elm = document_elm

features/steps/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def then_I_can_iterate_over_the_sections(context):
5757

5858
@then('the length of the section collection is 3')
5959
def then_the_length_of_the_section_collection_is_3(context):
60-
sections = context.sections
60+
sections = context.document.sections
6161
assert len(sections) == 3, (
6262
'expected len(sections) of 2, got %s' % len(sections)
6363
)

tests/parts/test_document.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def it_provides_access_to_the_document_paragraphs(
113113
paragraphs = document_part.paragraphs
114114
assert paragraphs is paragraphs_
115115

116+
def it_provides_access_to_the_document_sections(self, sections_fixture):
117+
document, document_elm, Sections_ = sections_fixture
118+
sections = document.sections
119+
Sections_.assert_called_once_with(document_elm)
120+
assert sections is Sections_.return_value
121+
116122
def it_provides_access_to_the_document_tables(self, tables_fixture):
117123
document_part, tables_ = tables_fixture
118124
tables = document_part.tables
@@ -300,6 +306,16 @@ def relate_to_(self, request, rId_):
300306
def rId_(self, request):
301307
return instance_mock(request, str)
302308

309+
@pytest.fixture
310+
def Sections_(self, request):
311+
return class_mock(request, 'docx.parts.document.Sections')
312+
313+
@pytest.fixture
314+
def sections_fixture(self, request, Sections_):
315+
document_elm = a_document().with_nsdecls().element
316+
document = DocumentPart(None, None, document_elm, None)
317+
return document, document_elm, Sections_
318+
303319
@pytest.fixture
304320
def serialize_part_xml_(self, request):
305321
return function_mock(

tests/test_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def it_provides_access_to_the_document_paragraphs(
115115
paragraphs = document.paragraphs
116116
assert paragraphs is paragraphs_
117117

118+
def it_provides_access_to_the_document_sections(self, document):
119+
body = document.sections
120+
assert body is document._document_part.sections
121+
118122
def it_provides_access_to_the_document_tables(self, tables_fixture):
119123
document, tables_ = tables_fixture
120124
tables = document.tables

0 commit comments

Comments
 (0)