Skip to content

Commit c7e8c7e

Browse files
author
Steve Canny
committed
tbl: add Table.columns
1 parent c04bdde commit c7e8c7e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

docx/table.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def add_row(self):
3737
tr.add_tc()
3838
return _Row(tr)
3939

40+
@lazyproperty
41+
def columns(self):
42+
return _ColumnCollection(self._tbl)
43+
4044
@lazyproperty
4145
def rows(self):
4246
return _RowCollection(self._tbl)
@@ -90,6 +94,9 @@ class _ColumnCollection(object):
9094
"""
9195
Sequence of |_Column| instances corresponding to the columns in a table.
9296
"""
97+
def __init__(self, tbl):
98+
super(_ColumnCollection, self).__init__()
99+
self._tbl = tbl
93100

94101

95102
class _Row(object):

tests/test_table.py

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

1111
from docx.table import (
12-
_Cell, _CellCollection, _Column, _Row, _RowCollection, Table
12+
_Cell, _CellCollection, _Column, _ColumnCollection, _Row, _RowCollection,
13+
Table
1314
)
1415

1516
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
@@ -18,11 +19,14 @@
1819

1920
class DescribeTable(object):
2021

21-
def it_provides_access_to_the_table_rows(self, row_access_fixture):
22-
table = row_access_fixture
22+
def it_provides_access_to_the_table_rows(self, table):
2323
rows = table.rows
2424
assert isinstance(rows, _RowCollection)
2525

26+
def it_provides_access_to_the_table_columns(self, table):
27+
columns = table.columns
28+
assert isinstance(columns, _ColumnCollection)
29+
2630
def it_can_add_a_column(self, add_column_fixture):
2731
table, expected_xml = add_column_fixture
2832
col = table.add_column()
@@ -52,7 +56,7 @@ def add_row_fixture(self):
5256
return table, expected_xml
5357

5458
@pytest.fixture
55-
def row_access_fixture(self):
59+
def table(self):
5660
tbl = _tbl_bldr(rows=2, cols=2).element
5761
table = Table(tbl)
5862
return table

0 commit comments

Comments
 (0)