Skip to content

Commit a03a35d

Browse files
author
Steve Canny
committed
tbl: add Table.rows
Also migrate _tbl_bldr() methods to module level so they can be shared by Describe_RowCollection.
1 parent ad718a0 commit a03a35d

2 files changed

Lines changed: 57 additions & 24 deletions

File tree

docx/table.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
The |Table| object and related proxy classes.
55
"""
66

7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
from .shared import lazyproperty
10+
711

812
class Table(object):
913
"""
@@ -33,6 +37,10 @@ def add_row(self):
3337
tr.add_tc()
3438
return _Row(tr)
3539

40+
@lazyproperty
41+
def rows(self):
42+
return _RowCollection(self._tbl)
43+
3644

3745
class _Column(object):
3846
"""
@@ -50,3 +58,12 @@ class _Row(object):
5058
def __init__(self, tr):
5159
super(_Row, self).__init__()
5260
self._tr = tr
61+
62+
63+
class _RowCollection(object):
64+
"""
65+
Sequence of |_Row| instances corresponding to the rows in a table.
66+
"""
67+
def __init__(self, tbl):
68+
super(_RowCollection, self).__init__()
69+
self._tbl = tbl

tests/test_table.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88

99
import pytest
1010

11-
from docx.table import _Column, _Row, Table
11+
from docx.table import _Column, _Row, _RowCollection, Table
1212

1313
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
1414
from .oxml.unitdata.text import a_p
1515

1616

1717
class DescribeTable(object):
1818

19+
def it_provides_access_to_the_table_rows(self, row_access_fixture):
20+
table = row_access_fixture
21+
rows = table.rows
22+
assert isinstance(rows, _RowCollection)
23+
1924
def it_can_add_a_column(self, add_column_fixture):
2025
table, expected_xml = add_column_fixture
2126
col = table.add_column()
@@ -32,34 +37,45 @@ def it_can_add_a_row(self, add_row_fixture):
3237

3338
@pytest.fixture
3439
def add_column_fixture(self):
35-
tbl = self._tbl_bldr(2, 1).element
40+
tbl = _tbl_bldr(2, 1).element
3641
table = Table(tbl)
37-
expected_xml = self._tbl_bldr(2, 2).xml()
42+
expected_xml = _tbl_bldr(2, 2).xml()
3843
return table, expected_xml
3944

4045
@pytest.fixture
4146
def add_row_fixture(self):
42-
tbl = self._tbl_bldr(rows=1, cols=2).element
47+
tbl = _tbl_bldr(rows=1, cols=2).element
4348
table = Table(tbl)
44-
expected_xml = self._tbl_bldr(rows=2, cols=2).xml()
49+
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
4550
return table, expected_xml
4651

47-
def _tbl_bldr(self, rows, cols):
48-
tblGrid_bldr = a_tblGrid()
49-
for i in range(cols):
50-
tblGrid_bldr.with_child(a_gridCol())
51-
tbl_bldr = a_tbl().with_nsdecls().with_child(tblGrid_bldr)
52-
for i in range(rows):
53-
tr_bldr = self._tr_bldr(cols)
54-
tbl_bldr.with_child(tr_bldr)
55-
return tbl_bldr
56-
57-
def _tc_bldr(self):
58-
return a_tc().with_child(a_p())
59-
60-
def _tr_bldr(self, cols):
61-
tr_bldr = a_tr()
62-
for i in range(cols):
63-
tc_bldr = self._tc_bldr()
64-
tr_bldr.with_child(tc_bldr)
65-
return tr_bldr
52+
@pytest.fixture
53+
def row_access_fixture(self):
54+
tbl = _tbl_bldr(rows=2, cols=2).element
55+
table = Table(tbl)
56+
return table
57+
58+
59+
# fixtures -----------------------------------------------------------
60+
61+
def _tbl_bldr(rows, cols):
62+
tblGrid_bldr = a_tblGrid()
63+
for i in range(cols):
64+
tblGrid_bldr.with_child(a_gridCol())
65+
tbl_bldr = a_tbl().with_nsdecls().with_child(tblGrid_bldr)
66+
for i in range(rows):
67+
tr_bldr = _tr_bldr(cols)
68+
tbl_bldr.with_child(tr_bldr)
69+
return tbl_bldr
70+
71+
72+
def _tc_bldr():
73+
return a_tc().with_child(a_p())
74+
75+
76+
def _tr_bldr(cols):
77+
tr_bldr = a_tr()
78+
for i in range(cols):
79+
tc_bldr = _tc_bldr()
80+
tr_bldr.with_child(tc_bldr)
81+
return tr_bldr

0 commit comments

Comments
 (0)