Skip to content

Commit ce3ecf7

Browse files
author
Steve Canny
committed
tbl: add Table.row_cells()
1 parent f919785 commit ce3ecf7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

docx/table.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def row_cells(self, row_idx):
7171
"""
7272
Sequence of cells in the row at *row_idx* in this table.
7373
"""
74-
raise NotImplementedError
74+
column_count = self._column_count
75+
start = row_idx * column_count
76+
end = start + column_count
77+
return self._cells[start:end]
7578

7679
@lazyproperty
7780
def rows(self):
@@ -103,6 +106,22 @@ def table(self):
103106
"""
104107
return self
105108

109+
@property
110+
def _cells(self):
111+
"""
112+
A sequence of |_Cell| objects, one for each cell of the layout grid.
113+
If the table contains a span, one or more |_Cell| object references
114+
are repeated.
115+
"""
116+
raise NotImplementedError
117+
118+
@property
119+
def _column_count(self):
120+
"""
121+
The number of grid columns in this table.
122+
"""
123+
raise NotImplementedError
124+
106125
@property
107126
def _tblPr(self):
108127
return self._tbl.tblPr

tests/test_table.py

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

42+
def it_provides_access_to_the_cells_in_a_row(self, row_cells_fixture):
43+
table, row_idx, expected_cells = row_cells_fixture
44+
row_cells = table.row_cells(row_idx)
45+
assert row_cells == expected_cells
46+
4247
def it_can_add_a_row(self, add_row_fixture):
4348
table, expected_xml = add_row_fixture
4449
row = table.add_row()
@@ -120,6 +125,15 @@ def autofit_set_fixture(self, request):
120125
expected_xml = xml(expected_tbl_cxml)
121126
return table, new_value, expected_xml
122127

128+
@pytest.fixture
129+
def row_cells_fixture(self, _cells_, _column_count_):
130+
table = Table(None, None)
131+
_cells_.return_value = [0, 1, 2, 3, 4, 5, 6, 7, 8]
132+
_column_count_.return_value = 3
133+
row_idx = 1
134+
expected_cells = [3, 4, 5]
135+
return table, row_idx, expected_cells
136+
123137
@pytest.fixture
124138
def table_fixture(self):
125139
table = Table(None, None)
@@ -152,6 +166,14 @@ def table_style_set_fixture(self, request):
152166

153167
# fixture components ---------------------------------------------
154168

169+
@pytest.fixture
170+
def _cells_(self, request):
171+
return property_mock(request, Table, '_cells')
172+
173+
@pytest.fixture
174+
def _column_count_(self, request):
175+
return property_mock(request, Table, '_column_count')
176+
155177
@pytest.fixture
156178
def table(self):
157179
tbl = _tbl_bldr(rows=2, cols=2).element

0 commit comments

Comments
 (0)