Skip to content

Commit f919785

Browse files
author
Steve Canny
committed
tbl: add _Row._index
1 parent b5c4a5e commit f919785

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

docx/oxml/table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class CT_Row(BaseOxmlElement):
2424
"""
2525
tc = ZeroOrMore('w:tc')
2626

27+
@property
28+
def tr_idx(self):
29+
"""
30+
The index of this ``<w:tr>`` element within its parent ``<w:tbl>``
31+
element.
32+
"""
33+
return self.getparent().tr_lst.index(self)
34+
2735
def _new_tc(self):
2836
return CT_Tc.new()
2937

docx/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def _index(self):
332332
"""
333333
Index of this row in its table, starting from zero.
334334
"""
335-
raise NotImplementedError
335+
return self._tr.tr_idx
336336

337337

338338
class _RowCells(Parented):

tests/test_table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ def it_provides_access_to_the_table_it_belongs_to(self, table_fixture):
454454
row, table_ = table_fixture
455455
assert row.table is table_
456456

457+
def it_knows_its_index_in_table_to_help(self, idx_fixture):
458+
row, expected_idx = idx_fixture
459+
assert row._index == expected_idx
460+
457461
# fixtures -------------------------------------------------------
458462

459463
@pytest.fixture
@@ -464,6 +468,13 @@ def cells_fixture(self, _index_, table_prop_, table_):
464468
table_.row_cells.return_value = list(expected_cells)
465469
return row, row_idx, expected_cells
466470

471+
@pytest.fixture
472+
def idx_fixture(self):
473+
tbl = element('w:tbl/(w:tr,w:tr,w:tr)')
474+
tr, expected_idx = tbl[1], 1
475+
row = _Row(tr, None)
476+
return row, expected_idx
477+
467478
@pytest.fixture
468479
def table_fixture(self, parent_, table_):
469480
row = _Row(None, parent_)

0 commit comments

Comments
 (0)