Skip to content

Commit 6113e40

Browse files
author
Steve Canny
committed
tbl: add _Cell.width setter
1 parent fa30e11 commit 6113e40

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

docx/oxml/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ def width(self):
212212
return None
213213
return tcPr.width
214214

215+
@width.setter
216+
def width(self, value):
217+
tcPr = self.get_or_add_tcPr()
218+
tcPr.width = value
219+
215220

216221
class CT_TcPr(BaseOxmlElement):
217222
"""

docx/table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def width(self):
128128
"""
129129
return self._tc.width
130130

131+
@width.setter
132+
def width(self, value):
133+
self._tc.width = value
134+
131135

132136
class _Column(Parented):
133137
"""

features/tbl-cell-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Feature: Get and set table cell properties
1414
| 1 inch | 1 inch |
1515

1616

17-
@wip
1817
Scenario Outline: Set cell width
1918
Given a table cell having a width of <width-setting>
2019
When I set the cell width to <new-setting>

tests/test_table.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010

11+
from docx.shared import Inches
1112
from docx.table import (
1213
_Cell, _Column, _ColumnCells, _Columns, _Row, _RowCells, _Rows, Table
1314
)
@@ -172,6 +173,12 @@ def it_knows_its_width_in_EMU(self, width_get_fixture):
172173
cell, expected_width = width_get_fixture
173174
assert cell.width == expected_width
174175

176+
def it_can_change_its_width(self, width_set_fixture):
177+
cell, value, expected_xml = width_set_fixture
178+
cell.width = value
179+
assert cell.width == value
180+
assert cell._tc.xml == expected_xml
181+
175182
# fixtures -------------------------------------------------------
176183

177184
@pytest.fixture
@@ -203,6 +210,18 @@ def width_get_fixture(self, request):
203210
cell = _Cell(element(tc_cxml), None)
204211
return cell, expected_width
205212

213+
@pytest.fixture(params=[
214+
('w:tc', Inches(1),
215+
'w:tc/w:tcPr/w:tcW{w:w=1440,w:type=dxa}'),
216+
('w:tc/w:tcPr/w:tcW{w:w=25%,w:type=pct}', Inches(2),
217+
'w:tc/w:tcPr/w:tcW{w:w=2880,w:type=dxa}'),
218+
])
219+
def width_set_fixture(self, request):
220+
tc_cxml, new_value, expected_cxml = request.param
221+
cell = _Cell(element(tc_cxml), None)
222+
expected_xml = xml(expected_cxml)
223+
return cell, new_value, expected_xml
224+
206225

207226
class Describe_Column(object):
208227

0 commit comments

Comments
 (0)