Skip to content

Commit 2fda570

Browse files
author
Steve Canny
committed
tbl: add Table.column_cells()
1 parent 0e3e2be commit 2fda570

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

docx/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def column_cells(self, column_idx):
6565
"""
6666
Sequence of cells in the column at *column_idx* in this table.
6767
"""
68-
raise NotImplementedError
68+
cells = self._cells
69+
idxs = range(column_idx, len(cells), self._column_count)
70+
return [cells[idx] for idx in idxs]
6971

7072
@lazyproperty
7173
def columns(self):

features/tbl-cell-access.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Access table cells
1515
| a combined span | 1 2 3 4 4 6 4 4 9 |
1616

1717

18-
@wip
1918
Scenario Outline: Access cell sequence of a column
2019
Given a 3x3 table having <span-state>
2120
Then the column cells text is <expected-text>
@@ -28,7 +27,6 @@ Feature: Access table cells
2827
| a combined span | 1 4 4 2 4 4 3 6 9 |
2928

3029

31-
@wip
3230
Scenario Outline: Access cell by row and column index
3331
Given a 3x3 table having <span-state>
3432
Then table.cell(<row>, <col>).text is <expected-text>

tests/test_table.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def it_provides_access_to_a_cell_by_row_and_col_indices(self, table):
4141
tc = tr.tc_lst[col_idx]
4242
assert tc is cell._tc
4343

44+
def it_provides_access_to_the_cells_in_a_column(self, col_cells_fixture):
45+
table, column_idx, expected_cells = col_cells_fixture
46+
column_cells = table.column_cells(column_idx)
47+
assert column_cells == expected_cells
48+
4449
def it_provides_access_to_the_cells_in_a_row(self, row_cells_fixture):
4550
table, row_idx, expected_cells = row_cells_fixture
4651
row_cells = table.row_cells(row_idx)
@@ -155,6 +160,15 @@ def cells_fixture(self, request):
155160
table = Table(parse_xml(tbl_xml), None)
156161
return table, cell_count, unique_count, matches
157162

163+
@pytest.fixture
164+
def col_cells_fixture(self, _cells_, _column_count_):
165+
table = Table(None, None)
166+
_cells_.return_value = [0, 1, 2, 3, 4, 5, 6, 7, 8]
167+
_column_count_.return_value = 3
168+
column_idx = 1
169+
expected_cells = [1, 4, 7]
170+
return table, column_idx, expected_cells
171+
158172
@pytest.fixture
159173
def column_count_fixture(self):
160174
tbl_cxml = 'w:tbl/w:tblGrid/(w:gridCol,w:gridCol,w:gridCol)'

0 commit comments

Comments
 (0)