Skip to content

Commit e053724

Browse files
author
Steve Canny
committed
sect: add Sections.__getitem__()
1 parent e7fb78a commit e053724

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

docx/parts/document.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ def __init__(self, document_elm):
236236
self._document_elm = document_elm
237237

238238
def __getitem__(self, key):
239-
pass
239+
if isinstance(key, slice):
240+
sectPr_lst = self._document_elm.sectPr_lst[key]
241+
return [Section(sectPr) for sectPr in sectPr_lst]
242+
sectPr = self._document_elm.sectPr_lst[key]
243+
return Section(sectPr)
240244

241245
def __iter__(self):
242246
for sectPr in self._document_elm.sectPr_lst:

features/doc-access-sections.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Feature: Access document sections
1010
And the length of the section collection is 3
1111

1212

13-
@wip
1413
Scenario: Access section in section collection
1514
Given a section collection
1615
Then I can iterate over the sections

tests/parts/test_document.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,19 @@ def it_can_iterate_over_its_Section_instances(self, iter_fixture):
635635
assert isinstance(section, Section)
636636
assert section_count == expected_count
637637

638+
def it_can_access_its_Section_instances_by_index(self, index_fixture):
639+
sections, indicies = index_fixture
640+
assert len(sections[0:2]) == 2
641+
for index in indicies:
642+
assert isinstance(sections[index], Section)
643+
638644
# fixtures -------------------------------------------------------
639645

646+
@pytest.fixture
647+
def index_fixture(self, document_elm):
648+
sections = Sections(document_elm)
649+
return sections, [0, 1]
650+
640651
@pytest.fixture
641652
def iter_fixture(self, document_elm):
642653
sections = Sections(document_elm)

0 commit comments

Comments
 (0)