Skip to content

Commit 2210884

Browse files
author
Steve Canny
committed
tbl: change Table.rows.add() to Table.add_row()
1 parent c6befdc commit 2210884

4 files changed

Lines changed: 24 additions & 25 deletions

File tree

docx/parts/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def add_table(self, rows, cols):
146146
for i in range(cols):
147147
table.columns.add()
148148
for i in range(rows):
149-
table.rows.add()
149+
table.add_row()
150150
return table
151151

152152
def clear_content(self):

docx/table.py

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

21+
def add_row(self):
22+
"""
23+
Return a |_Row| instance, newly added bottom-most to the table.
24+
"""
25+
tr = self._tbl.add_tr()
26+
for gridCol in self._tbl.tblGrid.gridCol_lst:
27+
tr.add_tc()
28+
return _Row(tr)
29+
2130
def cell(self, row_idx, col_idx):
2231
"""
2332
Return |_Cell| instance correponding to table cell at *row_idx*,
@@ -267,13 +276,3 @@ def __iter__(self):
267276

268277
def __len__(self):
269278
return len(self._tbl.tr_lst)
270-
271-
def add(self):
272-
"""
273-
Return a |_Row| instance, newly added bottom-most to the table.
274-
"""
275-
tbl = self._tbl
276-
tr = tbl.add_tr()
277-
for gridCol in tbl.tblGrid.gridCol_lst:
278-
tr.add_tc()
279-
return _Row(tr)

features/tbl-add-row-or-col.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Add a row or column to a table
33
As an python-docx developer
44
I need methods to add a row or column
55

6-
@wip
76
Scenario: Add a row to a table
87
Given a 2 x 2 table
98
When I add a row to the table

tests/test_table.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,22 @@ def it_can_apply_a_table_style_by_name(self, table_style_set_fixture):
4747
table.style = style_name
4848
assert table._tbl.xml == expected_xml
4949

50+
def it_can_add_a_row(self, add_row_fixture):
51+
table, expected_xml = add_row_fixture
52+
row = table.add_row()
53+
assert table._tbl.xml == expected_xml
54+
assert isinstance(row, _Row)
55+
assert row._tr is table._tbl.tr_lst[1]
56+
5057
# fixtures -------------------------------------------------------
5158

59+
@pytest.fixture
60+
def add_row_fixture(self):
61+
tbl = _tbl_bldr(rows=1, cols=2).element
62+
table = Table(tbl)
63+
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
64+
return table, expected_xml
65+
5266
@pytest.fixture
5367
def table(self):
5468
tbl = _tbl_bldr(rows=2, cols=2).element
@@ -326,21 +340,8 @@ def it_raises_on_indexed_access_out_of_range(self, rows_fixture):
326340
too_high = row_count
327341
rows[too_high]
328342

329-
def it_can_add_a_row(self, add_row_fixture):
330-
rows, expected_xml = add_row_fixture
331-
row = rows.add()
332-
assert rows._tbl.xml == expected_xml
333-
assert isinstance(row, _Row)
334-
335343
# fixtures -------------------------------------------------------
336344

337-
@pytest.fixture
338-
def add_row_fixture(self):
339-
tbl = _tbl_bldr(rows=1, cols=2).element
340-
rows = _Rows(tbl)
341-
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
342-
return rows, expected_xml
343-
344345
@pytest.fixture
345346
def rows_fixture(self):
346347
row_count = 2

0 commit comments

Comments
 (0)