Skip to content

Commit 050e00b

Browse files
author
Steve Canny
committed
tbl: add Table.cell()
1 parent c07bda9 commit 050e00b

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

docx/table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def add_row(self):
3737
tr.add_tc()
3838
return _Row(tr)
3939

40+
def cell(self, row_idx, col_idx):
41+
"""
42+
Return |_Cell| instance correponding to table cell at *row_idx*,
43+
*col_idx* intersection, where (0, 0) is the top, left-most cell.
44+
"""
45+
row = self.rows[row_idx]
46+
return row.cells[col_idx]
47+
4048
@lazyproperty
4149
def columns(self):
4250
return _ColumnCollection(self._tbl)

features/steps/table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def given_a_table_row_having_two_cells(context):
6464

6565
# then =====================================================
6666

67+
@then('I can access a cell using its row and column indices')
68+
def then_can_access_cell_using_its_row_and_col_indices(context):
69+
table = context.table_
70+
for row_idx in range(2):
71+
for col_idx in range(2):
72+
cell = table.cell(row_idx, col_idx)
73+
assert isinstance(cell, _Cell)
74+
75+
6776
@then('I can access a collection cell by index')
6877
def then_can_access_collection_cell_by_index(context):
6978
cells = context.cells

features/tbl-item-access.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ Feature: Access table rows, columns, and cells
3232
Given a cell collection having two cells
3333
Then I can iterate over the cell collection
3434
And I can access a collection cell by index
35+
36+
Scenario: Access cell in table
37+
Given a table having two rows
38+
Then I can access a cell using its row and column indices

tests/test_table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ def it_provides_access_to_the_table_columns(self, table):
2727
columns = table.columns
2828
assert isinstance(columns, _ColumnCollection)
2929

30+
def it_provides_access_to_a_cell_by_row_and_col_indices(self, table):
31+
for row_idx in range(2):
32+
for col_idx in range(2):
33+
cell = table.cell(row_idx, col_idx)
34+
assert isinstance(cell, _Cell)
35+
tr = table._tbl.tr_lst[row_idx]
36+
tc = tr.tc_lst[col_idx]
37+
assert tc is cell._tc
38+
3039
def it_can_add_a_column(self, add_column_fixture):
3140
table, expected_xml = add_column_fixture
3241
col = table.add_column()

0 commit comments

Comments
 (0)