Skip to content

Commit 836bd55

Browse files
author
Steve Canny
committed
tbl: add Table.style getter
1 parent 0d4f9f3 commit 836bd55

File tree

11 files changed

+105
-39
lines changed

11 files changed

+105
-39
lines changed

docx/oxml/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ class ValidationError(Exception):
1919
# custom element class mappings
2020
# ===========================================================================
2121

22+
from docx.oxml.shared import CT_String
23+
2224
from docx.oxml.parts import CT_Body, CT_Document
2325
register_custom_element_class('w:body', CT_Body)
2426
register_custom_element_class('w:document', CT_Document)
2527

26-
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblGrid, CT_Tc
28+
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblGrid, CT_TblPr, CT_Tc
2729
register_custom_element_class('w:tbl', CT_Tbl)
2830
register_custom_element_class('w:tblGrid', CT_TblGrid)
31+
register_custom_element_class('w:tblPr', CT_TblPr)
32+
register_custom_element_class('w:tblStyle', CT_String)
2933
register_custom_element_class('w:tc', CT_Tc)
3034
register_custom_element_class('w:tr', CT_Row)
3135

32-
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_String, CT_Text
36+
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_Text
3337
register_custom_element_class('w:p', CT_P)
3438
register_custom_element_class('w:pPr', CT_PPr)
3539
register_custom_element_class('w:pStyle', CT_String)

docx/oxml/shared.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,29 @@ def xml(self):
8787
top.
8888
"""
8989
return serialize_for_reading(self)
90+
91+
92+
# ===========================================================================
93+
# shared custom element classes
94+
# ===========================================================================
95+
96+
class CT_String(OxmlBaseElement):
97+
"""
98+
Used for ``<w:pStyle>`` and ``<w:tblStyle>`` elements and others,
99+
containing a style name in its ``val`` attribute.
100+
"""
101+
@classmethod
102+
def new_pStyle(cls, val):
103+
"""
104+
Return a new ``<w:pStyle>`` element with ``val`` attribute set to
105+
*val*.
106+
"""
107+
return OxmlElement('w:pStyle', attrs={qn('w:val'): val})
108+
109+
@property
110+
def val(self):
111+
return self.get(qn('w:val'))
112+
113+
@val.setter
114+
def val(self, val):
115+
return self.set(qn('w:val'), val)

docx/oxml/table.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ def tblGrid(self):
7878
raise ValidationError('required w:tblGrid child not found')
7979
return tblGrid
8080

81+
@property
82+
def tblPr(self):
83+
tblPr = self.find(qn('w:tblPr'))
84+
if tblPr is None:
85+
raise ValidationError('required w:tblPr child not found')
86+
return tblPr
87+
8188
@property
8289
def tr_lst(self):
8390
"""
@@ -170,6 +177,13 @@ def new(cls):
170177
"""
171178
return OxmlElement('w:tblPr')
172179

180+
@property
181+
def tblStyle(self):
182+
"""
183+
Optional <w:tblStyle> child element, or |None| if not present.
184+
"""
185+
return self.find(qn('w:tblStyle'))
186+
173187

174188
class CT_Tc(OxmlBaseElement):
175189
"""

docx/oxml/text.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from docx.oxml.shared import (
9-
nsdecls, OxmlBaseElement, OxmlElement, oxml_fromstring, qn
9+
CT_String, nsdecls, OxmlBaseElement, oxml_fromstring, qn
1010
)
1111

1212

@@ -179,28 +179,6 @@ def t_lst(self):
179179
return self.findall(qn('w:t'))
180180

181181

182-
class CT_String(OxmlBaseElement):
183-
"""
184-
Used for ``<w:pStyle>`` element, containing a style name in its ``val``
185-
attribute.
186-
"""
187-
@classmethod
188-
def new_pStyle(cls, val):
189-
"""
190-
Return a new ``<w:pStyle>`` element with ``val`` attribute set to
191-
*val*.
192-
"""
193-
return OxmlElement('w:pStyle', attrs={qn('w:val'): val})
194-
195-
@property
196-
def val(self):
197-
return self.get(qn('w:val'))
198-
199-
@val.setter
200-
def val(self, val):
201-
return self.set(qn('w:val'), val)
202-
203-
204182
class CT_Text(OxmlBaseElement):
205183
"""
206184
``<w:t>`` element, containing a sequence of characters within a run.

docx/table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def columns(self):
3434
def rows(self):
3535
return _RowCollection(self._tbl)
3636

37+
@property
38+
def style(self):
39+
tblStyle = self._tblPr.tblStyle
40+
if tblStyle is None:
41+
return None
42+
return tblStyle.val
43+
44+
@property
45+
def _tblPr(self):
46+
return self._tbl.tblPr
47+
3748

3849
class _Cell(object):
3950
"""

features/steps/table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ def then_can_get_length_of_row_cell_collection(context):
185185
@then('I can get the table style name')
186186
def then_can_get_table_style_name(context):
187187
table = context.table_
188-
assert table.style == 'foobar', "got '%s'" % table.style
188+
msg = "got '%s'" % table.style
189+
assert table.style == 'LightShading-Accent1', msg
189190

190191

191192
@then('I can iterate over the column cells')

features/tbl-style.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Query and apply a table style
33
As an python-docx developer
44
I need the ability to query and apply a table style
55

6-
@wip
76
Scenario: Access table style
87
Given a table having an applied style
98
Then I can get the table style name

tests/oxml/unitdata/shared.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test data builders shared by more than one other module
5+
"""
6+
7+
from ...unitdata import BaseBuilder
8+
9+
10+
class CT_StringBuilder(BaseBuilder):
11+
__nspfxs__ = ('w',)
12+
__attrs__ = ()
13+
14+
def __init__(self, tag):
15+
self.__tag__ = tag
16+
super(CT_StringBuilder, self).__init__()
17+
18+
def with_val(self, value):
19+
self._set_xmlattr('w:val', str(value))
20+
return self

tests/oxml/unitdata/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from ...unitdata import BaseBuilder
8+
from .shared import CT_StringBuilder
89

910

1011
class CT_RowBuilder(BaseBuilder):
@@ -59,6 +60,10 @@ def a_tblPr():
5960
return CT_TblPrBuilder()
6061

6162

63+
def a_tblStyle():
64+
return CT_StringBuilder('w:tblStyle')
65+
66+
6267
def a_tc():
6368
return CT_TcBuilder()
6469

tests/oxml/unitdata/text.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from ...unitdata import BaseBuilder
8+
from .shared import CT_StringBuilder
89

910

1011
class CT_PBuilder(BaseBuilder):
@@ -31,16 +32,6 @@ class CT_SectPrBuilder(BaseBuilder):
3132
__attrs__ = ()
3233

3334

34-
class CT_StringBuilder(BaseBuilder):
35-
__tag__ = 'w:pStyle'
36-
__nspfxs__ = ('w',)
37-
__attrs__ = ()
38-
39-
def with_val(self, value):
40-
self._set_xmlattr('w:val', str(value))
41-
return self
42-
43-
4435
class CT_TextBuilder(BaseBuilder):
4536
__tag__ = 'w:t'
4637
__nspfxs__ = ('w',)
@@ -56,7 +47,7 @@ def a_pPr():
5647

5748

5849
def a_pStyle():
59-
return CT_StringBuilder()
50+
return CT_StringBuilder('w:pStyle')
6051

6152

6253
def a_sectPr():

0 commit comments

Comments
 (0)