Skip to content

Commit 8ba64d4

Browse files
author
Steve Canny
committed
tbl: add _Column.cells
1 parent 806dbd4 commit 8ba64d4

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

docx/table.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def add_column(self):
2525
gridCol = tblGrid.add_gridCol()
2626
for tr in self._tbl.tr_lst:
2727
tr.add_tc()
28-
return _Column(gridCol)
28+
return _Column(gridCol, self._tbl)
2929

3030
def add_row(self):
3131
"""
@@ -67,16 +67,29 @@ class _Column(object):
6767
"""
6868
Table column
6969
"""
70-
def __init__(self, gridCol):
70+
def __init__(self, gridCol, tbl):
7171
super(_Column, self).__init__()
7272
self._gridCol = gridCol
73+
self._tbl = tbl
74+
75+
@lazyproperty
76+
def cells(self):
77+
"""
78+
|_ColumnCellCollection| instance containing sequence of |_Cell|
79+
instances corresponding to cells in this column.
80+
"""
81+
return _ColumnCellCollection(self._tbl, self._gridCol)
7382

7483

7584
class _ColumnCellCollection(object):
7685
"""
7786
Sequence of |_Cell| instances corresponding to the cells in a table
7887
column.
7988
"""
89+
def __init__(self, tbl, gridCol):
90+
super(_ColumnCellCollection, self).__init__()
91+
self._tbl = tbl
92+
self._gridCol = gridCol
8093

8194

8295
class _ColumnCollection(object):
@@ -96,10 +109,10 @@ def __getitem__(self, idx):
96109
except IndexError:
97110
msg = "column index [%d] is out of range" % idx
98111
raise IndexError(msg)
99-
return _Column(gridCol)
112+
return _Column(gridCol, self._tbl)
100113

101114
def __iter__(self):
102-
return (_Column(gridCol) for gridCol in self._gridCol_lst)
115+
return (_Column(gridCol, self._tbl) for gridCol in self._gridCol_lst)
103116

104117
def __len__(self):
105118
return len(self._gridCol_lst)
@@ -122,7 +135,7 @@ def __init__(self, tr):
122135
super(_Row, self).__init__()
123136
self._tr = tr
124137

125-
@property
138+
@lazyproperty
126139
def cells(self):
127140
"""
128141
Sequence of |_Cell| instances corresponding to the cells in this row.

tests/test_table.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import pytest
1010

1111
from docx.table import (
12-
_Cell, _Column, _ColumnCollection, _Row, _RowCellCollection,
13-
_RowCollection, Table
12+
_Cell, _Column, _ColumnCellCollection, _ColumnCollection, _Row,
13+
_RowCellCollection, _RowCollection, Table
1414
)
1515

1616
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
@@ -71,6 +71,19 @@ def table(self):
7171
return table
7272

7373

74+
class Describe_Column(object):
75+
76+
def it_provides_access_to_the_column_cells(self, column):
77+
cells = column.cells
78+
assert isinstance(cells, _ColumnCellCollection)
79+
80+
# fixtures -------------------------------------------------------
81+
82+
@pytest.fixture
83+
def column(self):
84+
return _Column(None, None)
85+
86+
7487
class Describe_ColumnCollection(object):
7588

7689
def it_knows_how_many_columns_it_contains(self, columns_fixture):

0 commit comments

Comments
 (0)