Skip to content

Commit b618bf9

Browse files
author
Steve Canny
committed
tbl: add Table.style setter
1 parent 836bd55 commit b618bf9

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

docs/dev/analysis/features/table.rst

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,7 @@ Schema Definitions
165165
</xsd:sequence>
166166
</xsd:complexType>
167167

168-
<xsd:complexType name="CT_TblPr">
169-
<xsd:complexContent>
170-
<xsd:extension base="CT_TblPrBase">
171-
<xsd:sequence>
172-
<xsd:element name="tblPrChange" type="CT_TblPrChange" minOccurs="0"/>
173-
</xsd:sequence>
174-
</xsd:extension>
175-
</xsd:complexContent>
176-
</xsd:complexType>
177-
178-
<xsd:complexType name="CT_TblPrBase">
168+
<xsd:complexType name="CT_TblPr"> <!-- denormalized -->
179169
<xsd:sequence>
180170
<xsd:element name="tblStyle" type="CT_String" minOccurs="0"/>
181171
<xsd:element name="tblpPr" type="CT_TblPPr" minOccurs="0" maxOccurs="1"/>
@@ -194,8 +184,8 @@ Schema Definitions
194184
<xsd:element name="tblLook" type="CT_TblLook" minOccurs="0" maxOccurs="1"/>
195185
<xsd:element name="tblCaption" type="CT_String" minOccurs="0" maxOccurs="1"/>
196186
<xsd:element name="tblDescription" type="CT_String" minOccurs="0" maxOccurs="1"/>
187+
<xsd:element name="tblPrChange" type="CT_TblPrChange" minOccurs="0"/>
197188
</xsd:sequence>
198-
</xsd:complexType>
199189

200190
<xsd:complexType name="CT_TblWidth">
201191
<xsd:attribute name="w" type="ST_MeasurementOrPercent"/>

docx/oxml/shared.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ class CT_String(OxmlBaseElement):
9898
Used for ``<w:pStyle>`` and ``<w:tblStyle>`` elements and others,
9999
containing a style name in its ``val`` attribute.
100100
"""
101+
@classmethod
102+
def new(cls, nsptagname, val):
103+
"""
104+
Return a new ``CT_String`` element with tagname *nsptagname* and
105+
``val`` attribute set to *val*.
106+
"""
107+
return OxmlElement(nsptagname, attrs={qn('w:val'): val})
108+
101109
@classmethod
102110
def new_pStyle(cls, val):
103111
"""

docx/oxml/table.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from docx.oxml.shared import OxmlBaseElement, OxmlElement, qn
1010

1111
from . import ValidationError
12+
from .shared import CT_String
1213
from .text import CT_P
1314

1415

@@ -170,6 +171,14 @@ class CT_TblPr(OxmlBaseElement):
170171
``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that
171172
define table properties such as style and borders.
172173
"""
174+
def add_tblStyle(self, style_name):
175+
"""
176+
Return a new <w:tblStyle> element newly inserted in sequence among
177+
the existing child elements, respecting the schema definition.
178+
"""
179+
tblStyle = CT_String.new('w:tblStyle', style_name)
180+
return self._insert_tblStyle(tblStyle)
181+
173182
@classmethod
174183
def new(cls):
175184
"""
@@ -184,6 +193,15 @@ def tblStyle(self):
184193
"""
185194
return self.find(qn('w:tblStyle'))
186195

196+
def _insert_tblStyle(self, tblStyle):
197+
"""
198+
Return *tblStyle* after inserting it in sequence among the existing
199+
child elements. Assumes no ``<w:tblStyle>`` element is present.
200+
"""
201+
assert self.tblStyle is None
202+
self.insert(0, tblStyle)
203+
return tblStyle
204+
187205

188206
class CT_Tc(OxmlBaseElement):
189207
"""

docx/table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def style(self):
4141
return None
4242
return tblStyle.val
4343

44+
@style.setter
45+
def style(self, style_name):
46+
tblStyle = self._tblPr.tblStyle
47+
if tblStyle is None:
48+
self._tblPr.add_tblStyle(style_name)
49+
else:
50+
tblStyle.val = style_name
51+
4452
@property
4553
def _tblPr(self):
4654
return self._tbl.tblPr

features/tbl-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Feature: Query and apply a table style
77
Given a table having an applied style
88
Then I can get the table style name
99

10-
@wip
1110
Scenario: Apply table style
1211
Given a table
1312
When I apply a style to the table

tests/test_table.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def it_knows_its_table_style(self, table_style_fixture):
4343
table, style = table_style_fixture
4444
assert table.style == style
4545

46+
def it_can_apply_a_table_style_by_name(self, table_style_set_fixture):
47+
table, style_name, expected_xml = table_style_set_fixture
48+
table.style = style_name
49+
assert table._tbl.xml == expected_xml
50+
4651
# fixtures -------------------------------------------------------
4752

4853
@pytest.fixture
@@ -62,6 +67,21 @@ def table_style_fixture(self):
6267
table = Table(tbl)
6368
return table, style
6469

70+
@pytest.fixture
71+
def table_style_set_fixture(self):
72+
# table ------------------------
73+
tbl = a_tbl().with_nsdecls().with_child(a_tblPr()).element
74+
table = Table(tbl)
75+
# style_name -------------------
76+
style_name = 'foobar'
77+
# expected_xml -----------------
78+
expected_xml = (
79+
a_tbl().with_nsdecls().with_child(
80+
a_tblPr().with_child(
81+
a_tblStyle().with_val(style_name)))
82+
).xml()
83+
return table, style_name, expected_xml
84+
6585

6686
class Describe_Cell(object):
6787

0 commit comments

Comments
 (0)