Skip to content

Commit 09a4e9b

Browse files
author
Steve Canny
committed
tbl: add Table.add_row()
1 parent d32fc11 commit 09a4e9b

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

docx/oxml/table.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def add_tc(self):
2424
tc = CT_Tc.new()
2525
return self._append_tc(tc)
2626

27+
@classmethod
28+
def new(cls):
29+
"""
30+
Return a new ``<w:tr>`` element.
31+
"""
32+
return OxmlElement('w:tr')
33+
2734
def _append_tc(self, tc):
2835
"""
2936
Return *tc* after appending it to end of tc sequence.
@@ -36,6 +43,14 @@ class CT_Tbl(OxmlBaseElement):
3643
"""
3744
``<w:tbl>`` element
3845
"""
46+
def add_tr(self):
47+
"""
48+
Return a new <w:tr> element that has been added at the end of any
49+
existing tr elements.
50+
"""
51+
tr = CT_Row.new()
52+
return self._append_tr(tr)
53+
3954
@property
4055
def tblGrid(self):
4156
tblGrid = self.find(qn('w:tblGrid'))
@@ -51,6 +66,13 @@ def tr_lst(self):
5166
"""
5267
return self.findall(qn('w:tr'))
5368

69+
def _append_tr(self, tr):
70+
"""
71+
Return *tr* after appending it to end of tr sequence.
72+
"""
73+
self.append(tr)
74+
return tr
75+
5476

5577
class CT_TblGrid(OxmlBaseElement):
5678
"""
@@ -65,6 +87,14 @@ def add_gridCol(self):
6587
gridCol = CT_TblGridCol.new()
6688
return self._append_gridCol(gridCol)
6789

90+
@property
91+
def gridCol_lst(self):
92+
"""
93+
Sequence containing the ``<w:gridCol>`` child elements in this
94+
``<w:tblGrid>``.
95+
"""
96+
return self.findall(qn('w:gridCol'))
97+
6898
def _append_gridCol(self, gridCol):
6999
"""
70100
Return *gridCol* after appending it to end of gridCol sequence.

docx/table.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def add_column(self):
2323
tr.add_tc()
2424
return _Column(gridCol)
2525

26+
def add_row(self):
27+
"""
28+
Return a |_Row| instance, newly added bottom-most to the table.
29+
"""
30+
tbl = self._tbl
31+
tr = tbl.add_tr()
32+
for gridCol in tbl.tblGrid.gridCol_lst:
33+
tr.add_tc()
34+
return _Row(tr)
35+
2636

2737
class _Column(object):
2838
"""
@@ -31,3 +41,12 @@ class _Column(object):
3141
def __init__(self, gridCol):
3242
super(_Column, self).__init__()
3343
self._gridCol = gridCol
44+
45+
46+
class _Row(object):
47+
"""
48+
Table row
49+
"""
50+
def __init__(self, tr):
51+
super(_Row, self).__init__()
52+
self._tr = tr

tests/test_table.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from docx.table import _Column, Table
11+
from docx.table import _Column, _Row, 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
@@ -19,10 +19,15 @@ class DescribeTable(object):
1919
def it_can_add_a_column(self, add_column_fixture):
2020
table, expected_xml = add_column_fixture
2121
col = table.add_column()
22-
print('\n%s' % table._tbl.xml)
2322
assert table._tbl.xml == expected_xml
2423
assert isinstance(col, _Column)
2524

25+
def it_can_add_a_row(self, add_row_fixture):
26+
table, expected_xml = add_row_fixture
27+
row = table.add_row()
28+
assert table._tbl.xml == expected_xml
29+
assert isinstance(row, _Row)
30+
2631
# fixtures -------------------------------------------------------
2732

2833
@pytest.fixture
@@ -32,6 +37,13 @@ def add_column_fixture(self):
3237
expected_xml = self._tbl_bldr(2, 2).xml()
3338
return table, expected_xml
3439

40+
@pytest.fixture
41+
def add_row_fixture(self):
42+
tbl = self._tbl_bldr(rows=1, cols=2).element
43+
table = Table(tbl)
44+
expected_xml = self._tbl_bldr(rows=2, cols=2).xml()
45+
return table, expected_xml
46+
3547
def _tbl_bldr(self, rows, cols):
3648
tblGrid_bldr = a_tblGrid()
3749
for i in range(cols):

0 commit comments

Comments
 (0)