Skip to content

Commit 15d4218

Browse files
author
Steve Canny
committed
blkct: add BlockItemContainer.paragraphs
* remove implementation from _Body to allow inheritance
1 parent 0c293d5 commit 15d4218

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

docx/blkcntnr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ def add_table(self, rows, cols):
5151
for i in range(rows):
5252
table.add_row()
5353
return table
54+
55+
@property
56+
def paragraphs(self):
57+
"""
58+
A list containing the paragraphs in this container, in document
59+
order. Read-only.
60+
"""
61+
return [Paragraph(p, self) for p in self._element.p_lst]

docx/parts/document.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from ..shape import InlineShape
1919
from ..shared import lazyproperty, Parented
2020
from ..table import Table
21-
from ..text import Paragraph
2221

2322

2423
class DocumentPart(XmlPart):
@@ -132,10 +131,6 @@ def clear_content(self):
132131
self._body.clear_content()
133132
return self
134133

135-
@property
136-
def paragraphs(self):
137-
return [Paragraph(p, self) for p in self._body.p_lst]
138-
139134
@property
140135
def tables(self):
141136
"""

tests/test_blkcntnr.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ def it_can_add_a_table(self, add_table_fixture):
2929
assert blkcntnr._element.xml == expected_xml
3030
assert isinstance(table, Table)
3131

32+
def it_provides_access_to_the_paragraphs_it_contains(
33+
self, paragraphs_fixture):
34+
# test len(), iterable, and indexed access
35+
blkcntnr, expected_count = paragraphs_fixture
36+
paragraphs = blkcntnr.paragraphs
37+
assert len(paragraphs) == expected_count
38+
count = 0
39+
for idx, paragraph in enumerate(paragraphs):
40+
assert isinstance(paragraph, Paragraph)
41+
assert paragraphs[idx] is paragraph
42+
count += 1
43+
assert count == expected_count
44+
3245
# fixtures -------------------------------------------------------
3346

3447
@pytest.fixture(params=[
@@ -62,3 +75,15 @@ def add_table_fixture(self, request):
6275
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
6376
expected_xml = xml(after_cxml)
6477
return blkcntnr, rows, cols, expected_xml
78+
79+
@pytest.fixture(params=[
80+
('w:body', 0),
81+
('w:body/w:p', 1),
82+
('w:body/(w:p,w:p)', 2),
83+
('w:body/(w:p,w:tbl)', 1),
84+
('w:body/(w:p,w:tbl,w:p)', 2),
85+
])
86+
def paragraphs_fixture(self, request):
87+
blkcntnr_cxml, expected_count = request.param
88+
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
89+
return blkcntnr, expected_count

0 commit comments

Comments
 (0)