Skip to content

Commit ef4d4ee

Browse files
author
Steve Canny
committed
acpt: add scenarios for Cell.width
1 parent f19d906 commit ef4d4ee

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

docx/table.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def text(self, text):
121121
r = p.add_r()
122122
r.text = text
123123

124+
@property
125+
def width(self):
126+
"""
127+
The width of this cell in EMU, or |None| if no explicit width is set.
128+
"""
129+
124130

125131
class _Column(Parented):
126132
"""

features/steps/table.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from behave import given, then, when
1010

1111
from docx import Document
12+
from docx.shared import Inches
1213
from docx.table import (
1314
_Cell, _Column, _ColumnCells, _Columns, _Row, _RowCells, _Rows
1415
)
@@ -56,6 +57,15 @@ def given_a_table(context):
5657
context.table_ = Document().add_table(rows=2, cols=2)
5758

5859

60+
@given('a table cell having a width of {width}')
61+
def given_a_table_cell_having_a_width_of_width(context, width):
62+
table_idx = {'no explicit setting': 0, '1 inch': 1, '2 inches': 2}[width]
63+
document = Document(test_docx('tbl-props'))
64+
table = document.tables[table_idx]
65+
cell = table.cell(0, 0)
66+
context.cell = cell
67+
68+
5969
@given('a table column having a width of {width_desc}')
6070
def given_a_table_having_a_width_of_width_desc(context, width_desc):
6171
col_idx = {
@@ -135,6 +145,12 @@ def when_apply_style_to_table(context):
135145
table.style = 'LightShading-Accent1'
136146

137147

148+
@when('I set the cell width to {width}')
149+
def when_I_set_the_cell_width_to_width(context, width):
150+
new_value = {'1 inch': Inches(1)}[width]
151+
context.cell.width = new_value
152+
153+
138154
@when('I set the column width to {width_emu}')
139155
def when_I_set_the_column_width_to_width_emu(context, width_emu):
140156
new_value = None if width_emu == 'None' else int(width_emu)
@@ -317,6 +333,15 @@ def then_the_reported_column_width_is_width_emu(context, width_emu):
317333
)
318334

319335

336+
@then('the reported width of the cell is {width}')
337+
def then_the_reported_width_of_the_cell_is_width(context, width):
338+
expected_width = {'None': None, '1 inch': Inches(1)}[width]
339+
actual_width = context.cell.width
340+
assert actual_width == expected_width, (
341+
'expected %s, got %s' % (expected_width, actual_width)
342+
)
343+
344+
320345
@then('the table style matches the name I applied')
321346
def then_table_style_matches_name_applied(context):
322347
table = context.table_
3 Bytes
Binary file not shown.

features/tbl-cell-props.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Feature: Get and set table cell properties
2+
In order to format a table cell to my requirements
3+
As an python-docx developer
4+
I need a way to get and set the properties of a table cell
5+
6+
7+
@wip
8+
Scenario Outline: Get cell width
9+
Given a table cell having a width of <width-setting>
10+
Then the reported width of the cell is <reported-width>
11+
12+
Examples: Table cell width settings
13+
| width-setting | reported-width |
14+
| no explicit setting | None |
15+
| 1 inch | 1 inch |
16+
17+
18+
@wip
19+
Scenario Outline: Set cell width
20+
Given a table cell having a width of <width-setting>
21+
When I set the cell width to <new-setting>
22+
Then the reported width of the cell is <reported-width>
23+
24+
Examples: table column width values
25+
| width-setting | new-setting | reported-width |
26+
| no explicit setting | 1 inch | 1 inch |
27+
| 2 inches | 1 inch | 1 inch |

0 commit comments

Comments
 (0)