Skip to content

Commit 0528a78

Browse files
author
Steve Canny
committed
acpt: add scenarios for Table.autofit get and set
1 parent 0984e9a commit 0528a78

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

docx/table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def add_row(self):
3838
tr.add_tc()
3939
return _Row(tr, self)
4040

41+
@property
42+
def autofit(self):
43+
"""
44+
|True| if column widths can be automatically adjusted to improve the
45+
fit of cell contents. |False| if table layout is fixed. Column widths
46+
are adjusted in either case if total column width exceeds page width.
47+
Read/write boolean.
48+
"""
49+
4150
def cell(self, row_idx, col_idx):
4251
"""
4352
Return |_Cell| instance correponding to table cell at *row_idx*,

features/steps/table.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ def given_a_table_having_an_applied_style(context):
7474
context.table_ = document.tables[0]
7575

7676

77+
@given('a table having an autofit layout of {autofit}')
78+
def given_a_table_having_an_autofit_layout_of_autofit(context, autofit):
79+
tbl_idx = {
80+
'no explicit setting': 0,
81+
'autofit': 1,
82+
'fixed': 2,
83+
}[autofit]
84+
document = Document(test_docx('tbl-props'))
85+
context.table_ = document.tables[tbl_idx]
86+
87+
7788
@given('a table having two columns')
7889
def given_a_table_having_two_columns(context):
7990
docx_path = test_docx('blk-containing-table')
@@ -130,6 +141,13 @@ def when_I_set_the_column_width_to_width_emu(context, width_emu):
130141
context.column.width = new_value
131142

132143

144+
@when('I set the table autofit to {setting}')
145+
def when_I_set_the_table_autofit_to_setting(context, setting):
146+
new_value = {'autofit': True, 'fixed': False}[setting]
147+
table = context.table_
148+
table.autofit = new_value
149+
150+
133151
# then =====================================================
134152

135153
@then('I can access a cell using its row and column indices')
@@ -284,6 +302,13 @@ def then_new_row_has_2_cells(context):
284302
assert len(context.row.cells) == 2
285303

286304

305+
@then('the reported autofit setting is {autofit}')
306+
def then_the_reported_autofit_setting_is_autofit(context, autofit):
307+
expected_value = {'autofit': True, 'fixed': False}[autofit]
308+
table = context.table_
309+
assert table.autofit is expected_value
310+
311+
287312
@then('the reported column width is {width_emu}')
288313
def then_the_reported_column_width_is_width_emu(context, width_emu):
289314
expected_value = None if width_emu == 'None' else int(width_emu)
13.7 KB
Binary file not shown.

features/tbl-props.feature

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

0 commit comments

Comments
 (0)