Skip to content

Commit ee130dc

Browse files
committed
blk: add BlockItemContainer.iter_inner_content()
1 parent 24e4c1b commit ee130dc

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

features/blk-iter-inner-content.feature

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@ Feature: Iterate paragraphs and tables in document-order
44
I need the ability to iterate the inner-content of a block-item-container
55

66

7-
@wip
87
Scenario: Document.iter_inner_content()
98
Given a Document object with paragraphs and tables
109
Then document.iter_inner_content() produces the block-items in document order
1110

1211

13-
@wip
1412
Scenario: Header.iter_inner_content()
1513
Given a Header object with paragraphs and tables
1614
Then header.iter_inner_content() produces the block-items in document order
1715

1816

19-
@wip
2017
Scenario: Footer.iter_inner_content()
2118
Given a Footer object with paragraphs and tables
2219
Then footer.iter_inner_content() produces the block-items in document order
2320

2421

25-
@wip
2622
Scenario: _Cell.iter_inner_content()
2723
Given a _Cell object with paragraphs and tables
2824
Then cell.iter_inner_content() produces the block-items in document order

src/docx/blkcntnr.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing_extensions import TypeAlias
1414

1515
from docx.oxml.table import CT_Tbl
16+
from docx.oxml.text.paragraph import CT_P
1617
from docx.shared import StoryChild
1718
from docx.text.paragraph import Paragraph
1819

@@ -73,7 +74,14 @@ def add_table(self, rows: int, cols: int, width: Length) -> Table:
7374

7475
def iter_inner_content(self) -> Iterator[Paragraph | Table]:
7576
"""Generate each `Paragraph` or `Table` in this container in document order."""
76-
raise NotImplementedError
77+
from docx.table import Table
78+
79+
for element in self._element.inner_content_elements:
80+
yield (
81+
Paragraph(element, self)
82+
if isinstance(element, CT_P)
83+
else Table(element, self)
84+
)
7785

7886
@property
7987
def paragraphs(self):

tests/test_blkcntnr.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import pytest
44

5+
from docx import Document
56
from docx.blkcntnr import BlockItemContainer
67
from docx.shared import Inches
78
from docx.table import Table
89
from docx.text.paragraph import Paragraph
910

1011
from .unitutil.cxml import element, xml
11-
from .unitutil.file import snippet_seq
12+
from .unitutil.file import snippet_seq, test_file
1213
from .unitutil.mock import call, instance_mock, method_mock
1314

1415

@@ -34,6 +35,26 @@ def it_can_add_a_table(self, add_table_fixture):
3435
assert table._element.xml == expected_xml
3536
assert table._parent is blkcntnr
3637

38+
def it_can_iterate_its_inner_content(self):
39+
document = Document(test_file("blk-inner-content.docx"))
40+
41+
inner_content = document.iter_inner_content()
42+
43+
para = next(inner_content)
44+
assert isinstance(para, Paragraph)
45+
assert para.text == "P1"
46+
# --
47+
t = next(inner_content)
48+
assert isinstance(t, Table)
49+
assert t.rows[0].cells[0].text == "T2"
50+
# --
51+
para = next(inner_content)
52+
assert isinstance(para, Paragraph)
53+
assert para.text == "P3"
54+
# --
55+
with pytest.raises(StopIteration):
56+
next(inner_content)
57+
3758
def it_provides_access_to_the_paragraphs_it_contains(self, paragraphs_fixture):
3859
# test len(), iterable, and indexed access
3960
blkcntnr, expected_count = paragraphs_fixture
11.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)