Skip to content

Commit 50af0f6

Browse files
author
Steve Canny
committed
style: add _Style.name getter
1 parent 21cd98c commit 50af0f6

5 files changed

Lines changed: 32 additions & 1 deletion

File tree

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
8787
register_element_cls('w:startOverride', CT_DecimalNumber)
8888

8989
from .parts.styles import CT_Style, CT_Styles
90+
register_element_cls('w:name', CT_String)
9091
register_element_cls('w:style', CT_Style)
9192
register_element_cls('w:styles', CT_Styles)
9293

docx/oxml/parts/styles.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ class CT_Style(BaseOxmlElement):
2222
'w:personalCompose', 'w:personalReply', 'w:rsid', 'w:pPr', 'w:rPr',
2323
'w:tblPr', 'w:trPr', 'w:tcPr', 'w:tblStylePr'
2424
)
25+
name = ZeroOrOne('w:name', successors=_tag_seq[1:])
2526
pPr = ZeroOrOne('w:pPr', successors=_tag_seq[17:])
2627
type = OptionalAttribute('w:type', WD_STYLE_TYPE)
2728
styleId = OptionalAttribute('w:styleId', ST_String)
2829
del _tag_seq
2930

31+
@property
32+
def name_val(self):
33+
"""
34+
Value of ``<w:name>`` child or |None| if not present.
35+
"""
36+
name = self.name
37+
if name is None:
38+
return None
39+
return name.val
40+
3041

3142
class CT_Styles(BaseOxmlElement):
3243
"""

docx/styles/style.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class BaseStyle(ElementProxy):
3535

3636
__slots__ = ()
3737

38+
@property
39+
def name(self):
40+
"""
41+
The UI name of this style.
42+
"""
43+
return self._element.name_val
44+
3845
@property
3946
def style_id(self):
4047
"""

features/sty-style-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Get and set style properties
44
I need a set of read/write style properties
55

66

7-
@wip
87
Scenario: Get name
98
Given a style having a known name
109
Then style.name is the known name

tests/styles/test_style.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def it_knows_its_type(self, type_get_fixture):
108108
style, expected_value = type_get_fixture
109109
assert style.type == expected_value
110110

111+
def it_knows_its_name(self, name_get_fixture):
112+
style, expected_value = name_get_fixture
113+
assert style.name == expected_value
114+
111115
# fixture --------------------------------------------------------
112116

113117
@pytest.fixture(params=[
@@ -131,6 +135,15 @@ def id_set_fixture(self, request):
131135
expected_xml = xml(expected_style_cxml)
132136
return style, new_value, expected_xml
133137

138+
@pytest.fixture(params=[
139+
('w:style{w:type=table}', None),
140+
('w:style{w:type=table}/w:name{w:val=Boofar}', 'Boofar'),
141+
])
142+
def name_get_fixture(self, request):
143+
style_cxml, expected_value = request.param
144+
style = BaseStyle(element(style_cxml))
145+
return style, expected_value
146+
134147
@pytest.fixture(params=[
135148
('w:style', WD_STYLE_TYPE.PARAGRAPH),
136149
('w:style{w:type=paragraph}', WD_STYLE_TYPE.PARAGRAPH),

0 commit comments

Comments
 (0)