Skip to content

Commit 96ce9e3

Browse files
author
Steve Canny
committed
tbl: rewrite Table.add_row()
1 parent 8efe8ad commit 96ce9e3

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

docx/table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def add_row(self):
3737
tbl = self._tbl
3838
tr = tbl.add_tr()
3939
for gridCol in tbl.tblGrid.gridCol_lst:
40-
tr.add_tc()
40+
tc = tr.add_tc()
41+
tc.width = gridCol.w
4142
return _Row(tr, self)
4243

4344
@property

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

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

66

7-
@wip
87
Scenario: Add a row to a table
98
Given a 2 x 2 table
109
When I add a row to the table
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
2+
<w:tblPr>
3+
<w:tblW w:type="auto" w:w="0"/>
4+
<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
5+
</w:tblPr>
6+
<w:tblGrid>
7+
<w:gridCol w:w="720"/>
8+
<w:gridCol w:w="1440"/>
9+
</w:tblGrid>
10+
<w:tr>
11+
<w:tc>
12+
<w:tcPr>
13+
<w:tcW w:type="dxa" w:w="720"/>
14+
</w:tcPr>
15+
<w:p/>
16+
</w:tc>
17+
<w:tc>
18+
<w:tcPr>
19+
<w:tcW w:type="dxa" w:w="1440"/>
20+
</w:tcPr>
21+
<w:p/>
22+
</w:tc>
23+
</w:tr>
24+
<w:tr>
25+
<w:tc>
26+
<w:tcPr>
27+
<w:tcW w:type="dxa" w:w="720"/>
28+
</w:tcPr>
29+
<w:p/>
30+
</w:tc>
31+
<w:tc>
32+
<w:tcPr>
33+
<w:tcW w:type="dxa" w:w="1440"/>
34+
</w:tcPr>
35+
<w:p/>
36+
</w:tc>
37+
</w:tr>
38+
</w:tbl>
39+
40+
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
41+
<w:tblPr>
42+
<w:tblW w:type="auto" w:w="0"/>
43+
<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
44+
</w:tblPr>
45+
<w:tblGrid>
46+
<w:gridCol w:w="720"/>
47+
<w:gridCol w:w="1440"/>
48+
</w:tblGrid>
49+
<w:tr>
50+
<w:tc>
51+
<w:tcPr>
52+
<w:tcW w:type="dxa" w:w="720"/>
53+
</w:tcPr>
54+
<w:p/>
55+
</w:tc>
56+
<w:tc>
57+
<w:tcPr>
58+
<w:tcW w:type="dxa" w:w="1440"/>
59+
</w:tcPr>
60+
<w:p/>
61+
</w:tc>
62+
</w:tr>
63+
<w:tr>
64+
<w:tc>
65+
<w:tcPr>
66+
<w:tcW w:type="dxa" w:w="720"/>
67+
</w:tcPr>
68+
<w:p/>
69+
</w:tc>
70+
<w:tc>
71+
<w:tcPr>
72+
<w:tcW w:type="dxa" w:w="1440"/>
73+
</w:tcPr>
74+
<w:p/>
75+
</w:tc>
76+
</w:tr>
77+
<w:tr>
78+
<w:tc>
79+
<w:tcPr>
80+
<w:tcW w:type="dxa" w:w="720"/>
81+
</w:tcPr>
82+
<w:p/>
83+
</w:tc>
84+
<w:tc>
85+
<w:tcPr>
86+
<w:tcW w:type="dxa" w:w="1440"/>
87+
</w:tcPr>
88+
<w:p/>
89+
</w:tc>
90+
</w:tr>
91+
</w:tbl>

tests/test_table.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def it_can_add_a_row(self, add_row_fixture):
101101
row = table.add_row()
102102
assert table._tbl.xml == expected_xml
103103
assert isinstance(row, _Row)
104-
assert row._tr is table._tbl.tr_lst[1]
104+
assert row._tr is table._tbl.tr_lst[-1]
105+
assert row._parent is table
105106

106107
def it_can_add_a_column(self, add_column_fixture):
107108
table, expected_xml = add_column_fixture
@@ -131,9 +132,10 @@ def add_column_fixture(self):
131132

132133
@pytest.fixture
133134
def add_row_fixture(self):
134-
tbl = _tbl_bldr(rows=1, cols=2).element
135+
snippets = snippet_seq('add-row-col')
136+
tbl = parse_xml(snippets[0])
135137
table = Table(tbl, None)
136-
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
138+
expected_xml = snippets[1]
137139
return table, expected_xml
138140

139141
@pytest.fixture(params=[

0 commit comments

Comments
 (0)