Skip to content

Commit 6469b5a

Browse files
author
Steve Canny
committed
sect: add Sections.__len__()
1 parent c161243 commit 6469b5a

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

docx/oxml/parts/document.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class CT_Document(BaseOxmlElement):
1616
"""
1717
body = ZeroOrOne('w:body')
1818

19+
@property
20+
def sectPr_lst(self):
21+
"""
22+
Return a list containing a reference to each ``<w:sectPr>`` element
23+
in the document, in the order encountered.
24+
"""
25+
return self.xpath('.//w:sectPr')
26+
1927

2028
class CT_Body(BaseOxmlElement):
2129
"""

docx/parts/document.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from collections import Sequence
12+
1113
from ..opc.constants import RELATIONSHIP_TYPE as RT
1214
from ..opc.oxml import serialize_part_xml
1315
from ..opc.package import Part
@@ -223,11 +225,17 @@ def _inline_lst(self):
223225
return body.xpath(xpath)
224226

225227

226-
class Sections(object):
228+
class Sections(Sequence):
227229
"""
228230
Sequence of |Section| objects corresponding to the sections in the
229231
document.
230232
"""
231233
def __init__(self, document_elm):
232234
super(Sections, self).__init__()
233235
self._document_elm = document_elm
236+
237+
def __getitem__(self, key):
238+
pass
239+
240+
def __len__(self):
241+
return len(self._document_elm.sectPr_lst)

features/doc-access-sections.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access document sections
44
I need a way to access document sections
55

66

7-
@wip
87
Scenario: Access section collection of a document
98
Given a document having three sections
109
Then I can access the section collection of the document

tests/parts/test_document.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from docx.oxml.parts.document import CT_Body, CT_Document
1717
from docx.oxml.text import CT_R
1818
from docx.package import ImageParts, Package
19-
from docx.parts.document import _Body, DocumentPart, InlineShapes
19+
from docx.parts.document import _Body, DocumentPart, InlineShapes, Sections
2020
from docx.parts.image import ImagePart
2121
from docx.shape import InlineShape
2222
from docx.table import Table
@@ -27,7 +27,7 @@
2727
from ..oxml.unitdata.table import (
2828
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblW, a_tc, a_tr
2929
)
30-
from ..oxml.unitdata.text import a_p, a_sectPr, an_r
30+
from ..oxml.unitdata.text import a_p, a_pPr, a_sectPr, an_r
3131
from ..unitutil import (
3232
function_mock, class_mock, initializer_mock, instance_mock, loose_mock,
3333
method_mock, property_mock
@@ -617,3 +617,26 @@ def rId_(self, request):
617617
@pytest.fixture
618618
def shape_id_(self, request):
619619
return instance_mock(request, int)
620+
621+
622+
class DescribeSections(object):
623+
624+
def it_knows_how_many_sections_it_contains(self, len_fixture):
625+
sections, expected_len = len_fixture
626+
print(sections._document_elm.xml)
627+
assert len(sections) == expected_len
628+
629+
# fixtures -------------------------------------------------------
630+
631+
@pytest.fixture
632+
def len_fixture(self):
633+
document_elm = (
634+
a_document().with_nsdecls().with_child(
635+
a_body().with_child(
636+
a_p().with_child(
637+
a_pPr().with_child(
638+
a_sectPr()))).with_child(
639+
a_sectPr()))
640+
).element
641+
sections = Sections(document_elm)
642+
return sections, 2

0 commit comments

Comments
 (0)