X Tutup
Skip to content

Commit f19d906

Browse files
author
Steve Canny
committed
tbl: add Table.autofit setter
1 parent d571aae commit f19d906

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

docx/oxml/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ def autofit(self):
105105
return True
106106
return False if tblLayout.type == 'fixed' else True
107107

108+
@autofit.setter
109+
def autofit(self, value):
110+
tblLayout = self.get_or_add_tblLayout()
111+
tblLayout.type = 'autofit' if value else 'fixed'
112+
108113
@property
109114
def style(self):
110115
"""

docx/table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def autofit(self):
4848
"""
4949
return self._tblPr.autofit
5050

51+
@autofit.setter
52+
def autofit(self, value):
53+
self._tblPr.autofit = value
54+
5155
def cell(self, row_idx, col_idx):
5256
"""
5357
Return |_Cell| instance correponding to table cell at *row_idx*,

features/tbl-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Get and set table properties
1515
| fixed | fixed |
1616

1717

18-
@wip
1918
Scenario Outline: Set autofit layout setting
2019
Given a table having an autofit layout of <autofit-setting>
2120
When I set the table autofit to <new-setting>

tests/test_table.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def it_knows_whether_it_should_autofit(self, autofit_get_fixture):
6464
table, expected_value = autofit_get_fixture
6565
assert table.autofit is expected_value
6666

67+
def it_can_change_its_autofit_setting(self, autofit_set_fixture):
68+
table, new_value, expected_xml = autofit_set_fixture
69+
table.autofit = new_value
70+
assert table._tbl.xml == expected_xml
71+
6772
# fixtures -------------------------------------------------------
6873

6974
@pytest.fixture
@@ -91,6 +96,24 @@ def autofit_get_fixture(self, request):
9196
table = Table(element(tbl_cxml), None)
9297
return table, expected_autofit
9398

99+
@pytest.fixture(params=[
100+
('w:tbl/w:tblPr', True,
101+
'w:tbl/w:tblPr/w:tblLayout{w:type=autofit}'),
102+
('w:tbl/w:tblPr', False,
103+
'w:tbl/w:tblPr/w:tblLayout{w:type=fixed}'),
104+
('w:tbl/w:tblPr', None,
105+
'w:tbl/w:tblPr/w:tblLayout{w:type=fixed}'),
106+
('w:tbl/w:tblPr/w:tblLayout{w:type=fixed}', True,
107+
'w:tbl/w:tblPr/w:tblLayout{w:type=autofit}'),
108+
('w:tbl/w:tblPr/w:tblLayout{w:type=autofit}', False,
109+
'w:tbl/w:tblPr/w:tblLayout{w:type=fixed}'),
110+
])
111+
def autofit_set_fixture(self, request):
112+
tbl_cxml, new_value, expected_tbl_cxml = request.param
113+
table = Table(element(tbl_cxml), None)
114+
expected_xml = xml(expected_tbl_cxml)
115+
return table, new_value, expected_xml
116+
94117
@pytest.fixture(params=[
95118
('w:tbl/w:tblPr', None),
96119
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', 'foobar'),

0 commit comments

Comments
 (0)
X Tutup