Skip to content

Commit 0e3e2be

Browse files
author
Steve Canny
committed
tbl: add _Column._index
1 parent 7e22dc8 commit 0e3e2be

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
@@ -97,6 +97,14 @@ class CT_TblGridCol(BaseOxmlElement):
9797
"""
9898
w = OptionalAttribute('w:w', ST_TwipsMeasure)
9999

100+
@property
101+
def gridCol_idx(self):
102+
"""
103+
The index of this ``<w:gridCol>`` element within its parent
104+
``<w:tblGrid>`` element.
105+
"""
106+
return self.getparent().gridCol_lst.index(self)
107+
100108

101109
class CT_TblLayoutType(BaseOxmlElement):
102110
"""

docx/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def _index(self):
274274
"""
275275
Index of this column in its table, starting from zero.
276276
"""
277-
raise NotImplementedError
277+
return self._gridCol.gridCol_idx
278278

279279

280280
class _ColumnCells(Parented):

tests/test_table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ def it_can_change_its_width(self, width_set_fixture):
394394
assert column.width == value
395395
assert column._gridCol.xml == expected_xml
396396

397+
def it_knows_its_index_in_table_to_help(self, index_fixture):
398+
column, expected_idx = index_fixture
399+
assert column._index == expected_idx
400+
397401
# fixtures -------------------------------------------------------
398402

399403
@pytest.fixture
@@ -404,6 +408,13 @@ def cells_fixture(self, _index_, table_prop_, table_):
404408
table_.column_cells.return_value = list(expected_cells)
405409
return column, column_idx, expected_cells
406410

411+
@pytest.fixture
412+
def index_fixture(self):
413+
tbl = element('w:tbl/w:tblGrid/(w:gridCol,w:gridCol,w:gridCol)')
414+
gridCol, expected_idx = tbl.tblGrid[1], 1
415+
column = _Column(gridCol, tbl, None)
416+
return column, expected_idx
417+
407418
@pytest.fixture
408419
def table_fixture(self, parent_, table_):
409420
column = _Column(None, None, parent_)

0 commit comments

Comments
 (0)