Skip to content

Commit b9bb316

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

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

docx/blkcntnr.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ def paragraphs(self):
5959
order. Read-only.
6060
"""
6161
return [Paragraph(p, self) for p in self._element.p_lst]
62+
63+
@property
64+
def tables(self):
65+
"""
66+
A list containing the tables in this container, in document order.
67+
Read-only.
68+
"""
69+
from .table import Table
70+
return [Table(tbl, self) for tbl in self._element.tbl_lst]

docx/parts/document.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from ..section import Section
1818
from ..shape import InlineShape
1919
from ..shared import lazyproperty, Parented
20-
from ..table import Table
2120

2221

2322
class DocumentPart(XmlPart):
@@ -131,14 +130,6 @@ def clear_content(self):
131130
self._body.clear_content()
132131
return self
133132

134-
@property
135-
def tables(self):
136-
"""
137-
A sequence containing all the tables in the document, in the order
138-
they appear.
139-
"""
140-
return [Table(tbl, self) for tbl in self._body.tbl_lst]
141-
142133

143134
class InlineShapes(Parented):
144135
"""

tests/test_blkcntnr.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def it_provides_access_to_the_paragraphs_it_contains(
4242
count += 1
4343
assert count == expected_count
4444

45+
def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
46+
# test len(), iterable, and indexed access
47+
blkcntnr, expected_count = tables_fixture
48+
tables = blkcntnr.tables
49+
assert len(tables) == expected_count
50+
count = 0
51+
for idx, table in enumerate(tables):
52+
assert isinstance(table, Table)
53+
assert tables[idx] is table
54+
count += 1
55+
assert count == expected_count
56+
4557
# fixtures -------------------------------------------------------
4658

4759
@pytest.fixture(params=[
@@ -87,3 +99,15 @@ def paragraphs_fixture(self, request):
8799
blkcntnr_cxml, expected_count = request.param
88100
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
89101
return blkcntnr, expected_count
102+
103+
@pytest.fixture(params=[
104+
('w:body', 0),
105+
('w:body/w:tbl', 1),
106+
('w:body/(w:tbl,w:tbl)', 2),
107+
('w:body/(w:p,w:tbl)', 1),
108+
('w:body/(w:tbl,w:tbl,w:p)', 2),
109+
])
110+
def tables_fixture(self, request):
111+
blkcntnr_cxml, expected_count = request.param
112+
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
113+
return blkcntnr, expected_count

0 commit comments

Comments
 (0)