Skip to content

Commit d28e053

Browse files
author
Steve Canny
committed
tbl: refactor Table.add_column() to _ColColl.add()
1 parent 57516ae commit d28e053

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

docx/parts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def add_table(self, rows, cols):
6363
tbl = self._body.add_tbl()
6464
table = Table(tbl)
6565
for i in range(cols):
66-
table.add_column()
66+
table.columns.add()
6767
for i in range(rows):
6868
table.add_row()
6969
return table

docx/table.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ def __init__(self, tbl):
1717
super(Table, self).__init__()
1818
self._tbl = tbl
1919

20-
def add_column(self):
21-
"""
22-
Return a |_Column| instance, newly added rightmost to the table.
23-
"""
24-
tblGrid = self._tbl.tblGrid
25-
gridCol = tblGrid.add_gridCol()
26-
for tr in self._tbl.tr_lst:
27-
tr.add_tc()
28-
return _Column(gridCol, self._tbl)
29-
3020
def add_row(self):
3121
"""
3222
Return a |_Row| instance, newly added bottom-most to the table.
@@ -155,6 +145,16 @@ def _gridCol_lst(self):
155145
tblGrid = self._tbl.tblGrid
156146
return tblGrid.gridCol_lst
157147

148+
def add(self):
149+
"""
150+
Return a |_Column| instance, newly added rightmost to the table.
151+
"""
152+
tblGrid = self._tbl.tblGrid
153+
gridCol = tblGrid.add_gridCol()
154+
for tr in self._tbl.tr_lst:
155+
tr.add_tc()
156+
return _Column(gridCol, self._tbl)
157+
158158

159159
class _Row(object):
160160
"""

tests/test_table.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ def it_provides_access_to_a_cell_by_row_and_col_indices(self, table):
3636
tc = tr.tc_lst[col_idx]
3737
assert tc is cell._tc
3838

39-
def it_can_add_a_column(self, add_column_fixture):
40-
table, expected_xml = add_column_fixture
41-
col = table.add_column()
42-
assert table._tbl.xml == expected_xml
43-
assert isinstance(col, _Column)
44-
4539
def it_can_add_a_row(self, add_row_fixture):
4640
table, expected_xml = add_row_fixture
4741
row = table.add_row()
@@ -50,13 +44,6 @@ def it_can_add_a_row(self, add_row_fixture):
5044

5145
# fixtures -------------------------------------------------------
5246

53-
@pytest.fixture
54-
def add_column_fixture(self):
55-
tbl = _tbl_bldr(2, 1).element
56-
table = Table(tbl)
57-
expected_xml = _tbl_bldr(2, 2).xml()
58-
return table, expected_xml
59-
6047
@pytest.fixture
6148
def add_row_fixture(self):
6249
tbl = _tbl_bldr(rows=1, cols=2).element
@@ -153,8 +140,21 @@ def it_raises_on_indexed_access_out_of_range(self, columns_fixture):
153140
with pytest.raises(IndexError):
154141
columns[too_high]
155142

143+
def it_can_add_a_column(self, add_column_fixture):
144+
columns, expected_xml = add_column_fixture
145+
column = columns.add()
146+
assert columns._tbl.xml == expected_xml
147+
assert isinstance(column, _Column)
148+
156149
# fixtures -------------------------------------------------------
157150

151+
@pytest.fixture
152+
def add_column_fixture(self):
153+
tbl = _tbl_bldr(2, 1).element
154+
columns = _ColumnCollection(tbl)
155+
expected_xml = _tbl_bldr(2, 2).xml()
156+
return columns, expected_xml
157+
158158
@pytest.fixture
159159
def columns_fixture(self):
160160
column_count = 2

0 commit comments

Comments
 (0)