Skip to content

Commit 1b753b3

Browse files
author
Steve Canny
committed
tbl: add _Column.width setter
1 parent 756d179 commit 1b753b3

5 files changed

Lines changed: 44 additions & 17 deletions

File tree

docx/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def twips(self):
6262
"""
6363
The equivalent length expressed in twips (int).
6464
"""
65-
return int(round(self / float(self._EMUS_PER_TWIP)) + 0.1)
65+
return int(round(self / float(self._EMUS_PER_TWIP)))
6666

6767

6868
class Inches(Length):

docx/table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ def width(self):
140140
"""
141141
return self._gridCol.w
142142

143+
@width.setter
144+
def width(self, value):
145+
self._gridCol.w = value
146+
143147

144148
class _ColumnCells(object):
145149
"""

features/steps/table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ def then_new_row_has_2_cells(context):
287287
@then('the reported column width is {width_emu}')
288288
def then_the_reported_column_width_is_width_emu(context, width_emu):
289289
expected_value = None if width_emu == 'None' else int(width_emu)
290-
assert context.column.width == expected_value
290+
assert context.column.width == expected_value, (
291+
'got %s' % context.column.width
292+
)
291293

292294

293295
@then('the table style matches the name I applied')

features/tbl-col-props.feature

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ Feature: Get and set table column widths
1414
| 1440 | 914400 |
1515

1616

17-
@wip
1817
Scenario Outline: Set column width
1918
Given a table column having a width of <width>
2019
When I set the column width to <new-width>
21-
Then the reported column width is <new-width>
20+
Then the reported column width is <width-emu>
2221

2322
Examples: table column width values
24-
| width | new-width |
25-
| no explicit setting | None |
26-
| no explicit setting | 914400 |
27-
| 1440 | None |
28-
| 1440 | 914400 |
29-
| 1440 | 424242 |
23+
| width | new-width | width-emu |
24+
| no explicit setting | None | None |
25+
| no explicit setting | 914400 | 914400 |
26+
| 1440 | None | None |
27+
| 1440 | 914400 | 914400 |
28+
| 1440 | 424497 | 424180 |

tests/test_table.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,16 @@ def it_provides_access_to_the_column_cells(self, column):
168168
cells = column.cells
169169
assert isinstance(cells, _ColumnCells)
170170

171-
def it_knows_its_width_in_EMU(self, width_fixture):
172-
column, expected_width = width_fixture
171+
def it_knows_its_width_in_EMU(self, width_get_fixture):
172+
column, expected_width = width_get_fixture
173173
assert column.width == expected_width
174174

175+
def it_can_change_its_width(self, width_set_fixture):
176+
column, value, expected_xml = width_set_fixture
177+
column.width = value
178+
assert column.width == value
179+
assert column._gridCol.xml == expected_xml
180+
175181
# fixtures -------------------------------------------------------
176182

177183
@pytest.fixture(params=[
@@ -182,21 +188,37 @@ def it_knows_its_width_in_EMU(self, width_fixture):
182188
('12.5pt', 158750),
183189
(None, None),
184190
])
185-
def width_fixture(self, request):
191+
def width_get_fixture(self, request):
186192
w, expected_width = request.param
187-
gridCol_bldr = a_gridCol().with_nsdecls()
188-
if w is not None:
189-
gridCol_bldr.with_w(w)
190-
gridCol = gridCol_bldr.element
193+
gridCol = self.gridCol_bldr(w).element
191194
column = _Column(gridCol, None)
192195
return column, expected_width
193196

197+
@pytest.fixture(params=[
198+
(4242, None, None),
199+
(None, None, None),
200+
(4242, 914400, 1440),
201+
(None, 914400, 1440),
202+
])
203+
def width_set_fixture(self, request):
204+
initial_w, value, expected_w = request.param
205+
gridCol = self.gridCol_bldr(initial_w).element
206+
column = _Column(gridCol, None)
207+
expected_xml = self.gridCol_bldr(expected_w).xml()
208+
return column, value, expected_xml
209+
194210
# fixture components ---------------------------------------------
195211

196212
@pytest.fixture
197213
def column(self):
198214
return _Column(None, None)
199215

216+
def gridCol_bldr(self, w=None):
217+
gridCol_bldr = a_gridCol().with_nsdecls()
218+
if w is not None:
219+
gridCol_bldr.with_w(w)
220+
return gridCol_bldr
221+
200222

201223
class Describe_ColumnCells(object):
202224

0 commit comments

Comments
 (0)