Skip to content

Commit 8364dad

Browse files
author
Steve Canny
committed
tbl: add _Cell.tables
1 parent 2214533 commit 8364dad

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

docx/table.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,17 @@ def paragraphs(self):
130130
"""
131131
List of paragraphs in the cell. A table cell is required to contain
132132
at least one block-level element and end with a paragraph. By
133-
default, a new cell contains a single paragraph.
133+
default, a new cell contains a single paragraph. Read-only
134134
"""
135135
return super(_Cell, self).paragraphs
136136

137+
@property
138+
def tables(self):
139+
"""
140+
List of tables in the cell, in the order they appear. Read-only.
141+
"""
142+
return super(_Cell, self).tables
143+
137144
@write_only_property
138145
def text(self, text):
139146
"""

tests/test_table.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ def it_provides_access_to_the_paragraphs_it_contains(
175175
count += 1
176176
assert count == 2
177177

178+
def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
179+
# test len(), iterable, and indexed access
180+
cell, expected_count = tables_fixture
181+
tables = cell.tables
182+
assert len(tables) == expected_count
183+
count = 0
184+
for idx, table in enumerate(tables):
185+
assert isinstance(table, Table)
186+
assert tables[idx] is table
187+
count += 1
188+
assert count == expected_count
189+
178190
def it_can_replace_its_content_with_a_string_of_text(
179191
self, text_set_fixture):
180192
cell, text, expected_xml = text_set_fixture
@@ -223,6 +235,18 @@ def add_table_fixture(self, request):
223235
def paragraphs_fixture(self):
224236
return _Cell(element('w:tc/(w:p, w:p)'), None)
225237

238+
@pytest.fixture(params=[
239+
('w:tc', 0),
240+
('w:tc/w:tbl', 1),
241+
('w:tc/(w:tbl,w:tbl)', 2),
242+
('w:tc/(w:p,w:tbl)', 1),
243+
('w:tc/(w:tbl,w:tbl,w:p)', 2),
244+
])
245+
def tables_fixture(self, request):
246+
cell_cxml, expected_count = request.param
247+
cell = _Cell(element(cell_cxml), None)
248+
return cell, expected_count
249+
226250
@pytest.fixture(params=[
227251
('w:tc/w:p', 'foobar',
228252
'w:tc/w:p/w:r/w:t"foobar"'),

0 commit comments

Comments
 (0)