Skip to content

Commit cdb413c

Browse files
author
Steve Canny
committed
acpt: add Row.cells access scenario
1 parent f79e7f2 commit cdb413c

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

docx/table.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def rows(self):
4242
return _RowCollection(self._tbl)
4343

4444

45+
class _Cell(object):
46+
"""
47+
Table cell
48+
"""
49+
50+
51+
class _CellCollection(object):
52+
"""
53+
Sequence of |_Cell| instances corresponding to the cells in a table row.
54+
"""
55+
56+
4557
class _Column(object):
4658
"""
4759
Table column

features/steps/table.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from behave import given, then
1010

1111
from docx import Document
12-
from docx.table import _Row
12+
from docx.table import _Cell, _CellCollection, _Row
1313

1414
from .helpers import test_docx
1515

@@ -23,8 +23,30 @@ def given_a_table_having_two_rows(context):
2323
context.table_ = document.body.tables[0]
2424

2525

26+
@given('a table row having two cells')
27+
def given_a_table_row_having_two_cells(context):
28+
docx_path = test_docx('blk-containing-table')
29+
document = Document(docx_path)
30+
context.row = document.body.tables[0].rows[0]
31+
32+
2633
# then =====================================================
2734

35+
@then('I can access a collection cell by index')
36+
def then_can_access_collection_cell_by_index(context):
37+
cells = context.row.cells
38+
for idx in range(2):
39+
cell = cells[idx]
40+
assert isinstance(cell, _Cell)
41+
42+
43+
@then('I can access the cell collection of the row')
44+
def then_can_access_cell_collection_of_row(context):
45+
row = context.row
46+
cells = row.cells
47+
assert isinstance(cells, _CellCollection)
48+
49+
2850
@then('I can access the rows by index')
2951
def then_can_access_rows_by_index(context):
3052
rows = context.table_.rows
@@ -34,6 +56,23 @@ def then_can_access_rows_by_index(context):
3456
assert isinstance(row, _Row)
3557

3658

59+
@then('I can get the length of the cell collection')
60+
def then_can_get_length_of_cell_collection(context):
61+
row = context.row
62+
cells = row.cells
63+
assert len(cells) == 2
64+
65+
66+
@then('I can iterate over the cell collection')
67+
def then_can_iterate_over_cell_collection(context):
68+
row = context.row
69+
actual_count = 0
70+
for cell in row.cells:
71+
actual_count += 1
72+
assert isinstance(cell, _Cell)
73+
assert actual_count == 2
74+
75+
3776
@then('the length of its row collection is 2')
3877
def then_len_of_row_collection_is_2(context):
3978
rows = context.table_.rows

features/tbl-item-access.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ Feature: Access table rows, columns, and cells
1111
Scenario: Access table row by collection index
1212
Given a table having two rows
1313
Then I can access the rows by index
14+
15+
@wip
16+
Scenario: Access cell collection of table row
17+
Given a table row having two cells
18+
Then I can access the cell collection of the row
19+
And I can get the length of the cell collection
20+
And I can iterate over the cell collection
21+
And I can access a collection cell by index

0 commit comments

Comments
 (0)