Skip to content

Commit 7541f20

Browse files
author
Steve Canny
committed
tbl: rewrite Table.add_column()
Add width parameter.
1 parent 96ce9e3 commit 7541f20

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ NEXT
77
++++++++++++++++++
88

99
- Fix #149: KeyError on Document.add_table()
10+
- Fix #78: feature: add_table() sets cell widths
1011

1112

1213
0.8.4 (2015-02-20)

docx/table.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ def __init__(self, tbl, parent):
2020
super(Table, self).__init__(parent)
2121
self._element = self._tbl = tbl
2222

23-
def add_column(self):
23+
def add_column(self, width):
2424
"""
25-
Return a |_Column| instance, newly added rightmost to the table.
25+
Return a |_Column| object of *width*, newly added rightmost to the
26+
table.
2627
"""
2728
tblGrid = self._tbl.tblGrid
2829
gridCol = tblGrid.add_gridCol()
30+
gridCol.w = width
2931
for tr in self._tbl.tr_lst:
30-
tr.add_tc()
32+
tc = tr.add_tc()
33+
tc.width = width
3134
return _Column(gridCol, self)
3235

3336
def add_row(self):

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Feature: Add a row or column to a table
1212
And the width of each cell is 3.0 inches
1313

1414

15-
@wip
1615
Scenario: Add a column to a table
1716
Given a 2 x 2 table
1817
When I add a 1.0 inch column to the table

tests/test_files/snippets/add-row-col.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,55 @@
8989
</w:tc>
9090
</w:tr>
9191
</w:tbl>
92+
93+
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
94+
<w:tblPr>
95+
<w:tblW w:type="auto" w:w="0"/>
96+
<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
97+
</w:tblPr>
98+
<w:tblGrid>
99+
<w:gridCol w:w="720"/>
100+
<w:gridCol w:w="1440"/>
101+
<w:gridCol w:w="2160"/>
102+
</w:tblGrid>
103+
<w:tr>
104+
<w:tc>
105+
<w:tcPr>
106+
<w:tcW w:type="dxa" w:w="720"/>
107+
</w:tcPr>
108+
<w:p/>
109+
</w:tc>
110+
<w:tc>
111+
<w:tcPr>
112+
<w:tcW w:type="dxa" w:w="1440"/>
113+
</w:tcPr>
114+
<w:p/>
115+
</w:tc>
116+
<w:tc>
117+
<w:tcPr>
118+
<w:tcW w:type="dxa" w:w="2160"/>
119+
</w:tcPr>
120+
<w:p/>
121+
</w:tc>
122+
</w:tr>
123+
<w:tr>
124+
<w:tc>
125+
<w:tcPr>
126+
<w:tcW w:type="dxa" w:w="720"/>
127+
</w:tcPr>
128+
<w:p/>
129+
</w:tc>
130+
<w:tc>
131+
<w:tcPr>
132+
<w:tcW w:type="dxa" w:w="1440"/>
133+
</w:tcPr>
134+
<w:p/>
135+
</w:tc>
136+
<w:tc>
137+
<w:tcPr>
138+
<w:tcW w:type="dxa" w:w="2160"/>
139+
</w:tcPr>
140+
<w:p/>
141+
</w:tc>
142+
</w:tr>
143+
</w:tbl>

tests/test_table.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ def it_can_add_a_row(self, add_row_fixture):
105105
assert row._parent is table
106106

107107
def it_can_add_a_column(self, add_column_fixture):
108-
table, expected_xml = add_column_fixture
109-
column = table.add_column()
108+
table, width, expected_xml = add_column_fixture
109+
column = table.add_column(width)
110110
assert table._tbl.xml == expected_xml
111111
assert isinstance(column, _Column)
112-
assert column._gridCol is table._tbl.tblGrid.gridCol_lst[1]
112+
assert column._gridCol is table._tbl.tblGrid.gridCol_lst[-1]
113+
assert column._parent is table
113114

114115
def it_provides_access_to_its_cells_to_help(self, cells_fixture):
115116
table, cell_count, unique_count, matches = cells_fixture
@@ -125,10 +126,12 @@ def it_provides_access_to_its_cells_to_help(self, cells_fixture):
125126

126127
@pytest.fixture
127128
def add_column_fixture(self):
128-
tbl = _tbl_bldr(2, 1).element
129+
snippets = snippet_seq('add-row-col')
130+
tbl = parse_xml(snippets[0])
129131
table = Table(tbl, None)
130-
expected_xml = _tbl_bldr(2, 2).xml()
131-
return table, expected_xml
132+
width = Inches(1.5)
133+
expected_xml = snippets[2]
134+
return table, width, expected_xml
132135

133136
@pytest.fixture
134137
def add_row_fixture(self):

0 commit comments

Comments
 (0)