Skip to content

Commit a6bef96

Browse files
author
Steve Canny
committed
tbl: rewrite Table.style setter
1 parent d922569 commit a6bef96

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

docx/oxml/table.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ def tblStyle_val(self):
9696
return None
9797
return tblStyle.val
9898

99+
@tblStyle_val.setter
100+
def tblStyle_val(self, styleId):
101+
"""
102+
Set the value of `w:tblPr/w:tblStyle/@w:val` (a table style id) to
103+
*styleId*. If *styleId* is None, remove the `w:tblStyle` element.
104+
"""
105+
tblPr = self.tblPr
106+
tblPr._remove_tblStyle()
107+
if styleId is None:
108+
return
109+
tblPr._add_tblStyle().val = styleId
110+
99111
@classmethod
100112
def _tbl_xml(cls):
101113
return (

docx/table.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ def style(self):
125125
return self.part.get_style(style_id, WD_STYLE_TYPE.TABLE)
126126

127127
@style.setter
128-
def style(self, value):
129-
self._tblPr.style = value
128+
def style(self, style_or_name):
129+
style_id = self.part.get_style_id(
130+
style_or_name, WD_STYLE_TYPE.TABLE
131+
)
132+
self._tbl.tblStyle_val = style_id
130133

131134
@property
132135
def table(self):

features/api-add-table.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Feature: Add a table
1111
And table.style is styles['Light Shading Accent 1']
1212

1313

14-
@wip
1514
Scenario: Add a table specifying style
1615
Given a document
1716
When I add a 2 x 2 table specifying style 'Table Grid'

features/tbl-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Query and apply a table style
1515
| Light Shading - Accent 1 | Light Shading Accent 1 |
1616

1717

18-
@wip
1918
Scenario Outline: Apply a table style
2019
Given a table having <style> style
2120
When I assign <value> to table.style

tests/test_table.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ def it_knows_its_table_style(self, style_get_fixture):
5252
)
5353
assert style is style_
5454

55-
def it_can_apply_a_table_style_by_name(self, table_style_set_fixture):
56-
table, style_name, expected_xml = table_style_set_fixture
57-
table.style = style_name
55+
def it_can_change_its_table_style(self, style_set_fixture):
56+
table, value, expected_xml = style_set_fixture
57+
table.style = value
58+
table.part.get_style_id.assert_called_once_with(
59+
value, WD_STYLE_TYPE.TABLE
60+
)
5861
assert table._tbl.xml == expected_xml
5962

6063
def it_knows_it_is_the_table_its_children_belong_to(self, table_fixture):
@@ -233,26 +236,25 @@ def style_get_fixture(self, part_prop_):
233236
style_ = part_prop_.return_value.get_style.return_value
234237
return table, style_id, style_
235238

236-
@pytest.fixture
237-
def table_fixture(self):
238-
table = Table(None, None)
239-
return table
240-
241239
@pytest.fixture(params=[
242-
('w:tbl/w:tblPr', 'foobar',
243-
'w:tbl/w:tblPr/w:tblStyle{w:val=foobar}'),
244-
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', 'barfoo',
245-
'w:tbl/w:tblPr/w:tblStyle{w:val=barfoo}'),
246-
('w:tbl/w:tblPr/w:tblStyle{w:val=foobar}', None,
247-
'w:tbl/w:tblPr'),
248-
('w:tbl/w:tblPr', None,
240+
('w:tbl/w:tblPr', 'Tbl A', 'TblA',
241+
'w:tbl/w:tblPr/w:tblStyle{w:val=TblA}'),
242+
('w:tbl/w:tblPr/w:tblStyle{w:val=TblA}', 'Tbl B', 'TblB',
243+
'w:tbl/w:tblPr/w:tblStyle{w:val=TblB}'),
244+
('w:tbl/w:tblPr/w:tblStyle{w:val=TblB}', None, None,
249245
'w:tbl/w:tblPr'),
250246
])
251-
def table_style_set_fixture(self, request):
252-
tbl_cxml, new_style, expected_cxml = request.param
247+
def style_set_fixture(self, request, part_prop_):
248+
tbl_cxml, value, style_id, expected_cxml = request.param
253249
table = Table(element(tbl_cxml), None)
250+
part_prop_.return_value.get_style_id.return_value = style_id
254251
expected_xml = xml(expected_cxml)
255-
return table, new_style, expected_xml
252+
return table, value, expected_xml
253+
254+
@pytest.fixture
255+
def table_fixture(self):
256+
table = Table(None, None)
257+
return table
256258

257259
# fixture components ---------------------------------------------
258260

0 commit comments

Comments
 (0)