Skip to content

Commit e7fb78a

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

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

docx/parts/document.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..opc.oxml import serialize_part_xml
1515
from ..opc.package import Part
1616
from ..oxml import parse_xml
17+
from ..section import Section
1718
from ..shape import InlineShape
1819
from ..shared import lazyproperty, Parented
1920
from ..table import Table
@@ -237,5 +238,9 @@ def __init__(self, document_elm):
237238
def __getitem__(self, key):
238239
pass
239240

241+
def __iter__(self):
242+
for sectPr in self._document_elm.sectPr_lst:
243+
yield Section(sectPr)
244+
240245
def __len__(self):
241246
return len(self._document_elm.sectPr_lst)

docx/section.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ class Section(object):
1111
"""
1212
Document section, providing access to section and page setup settings.
1313
"""
14+
def __init__(self, sectPr):
15+
super(Section, self).__init__()
16+
self._sectPr = sectPr

tests/parts/test_document.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from docx.package import ImageParts, Package
1919
from docx.parts.document import _Body, DocumentPart, InlineShapes, Sections
2020
from docx.parts.image import ImagePart
21+
from docx.section import Section
2122
from docx.shape import InlineShape
2223
from docx.table import Table
2324
from docx.text import Paragraph
@@ -626,17 +627,35 @@ def it_knows_how_many_sections_it_contains(self, len_fixture):
626627
print(sections._document_elm.xml)
627628
assert len(sections) == expected_len
628629

630+
def it_can_iterate_over_its_Section_instances(self, iter_fixture):
631+
sections, expected_count = iter_fixture
632+
section_count = 0
633+
for section in sections:
634+
section_count += 1
635+
assert isinstance(section, Section)
636+
assert section_count == expected_count
637+
629638
# fixtures -------------------------------------------------------
630639

631640
@pytest.fixture
632-
def len_fixture(self):
633-
document_elm = (
641+
def iter_fixture(self, document_elm):
642+
sections = Sections(document_elm)
643+
return sections, 2
644+
645+
@pytest.fixture
646+
def len_fixture(self, document_elm):
647+
sections = Sections(document_elm)
648+
return sections, 2
649+
650+
# fixture components ---------------------------------------------
651+
652+
@pytest.fixture
653+
def document_elm(self):
654+
return (
634655
a_document().with_nsdecls().with_child(
635656
a_body().with_child(
636657
a_p().with_child(
637658
a_pPr().with_child(
638659
a_sectPr()))).with_child(
639660
a_sectPr()))
640661
).element
641-
sections = Sections(document_elm)
642-
return sections, 2

0 commit comments

Comments
 (0)