X Tutup
Skip to content

Commit ade2ff4

Browse files
author
Steve Canny
committed
style: add CT_Styles.add_style_of_type()
1 parent c8f7620 commit ade2ff4

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docx/oxml/styles.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111
)
1212

1313

14+
def styleId_from_name(name):
15+
"""
16+
Return the style id corresponding to *name*, taking into account
17+
special-case names such as 'Heading 1'.
18+
"""
19+
return {
20+
'caption': 'Caption',
21+
'heading 1': 'Heading1',
22+
'heading 2': 'Heading2',
23+
'heading 3': 'Heading3',
24+
'heading 4': 'Heading4',
25+
'heading 5': 'Heading5',
26+
'heading 6': 'Heading6',
27+
'heading 7': 'Heading7',
28+
'heading 8': 'Heading8',
29+
'heading 9': 'Heading9',
30+
}.get(name, name.replace(' ', ''))
31+
32+
1433
class CT_Style(BaseOxmlElement):
1534
"""
1635
A ``<w:style>`` element, representing a style definition
@@ -27,6 +46,7 @@ class CT_Style(BaseOxmlElement):
2746
type = OptionalAttribute('w:type', WD_STYLE_TYPE)
2847
styleId = OptionalAttribute('w:styleId', ST_String)
2948
default = OptionalAttribute('w:default', ST_OnOff)
49+
customStyle = OptionalAttribute('w:customStyle', ST_OnOff)
3050
del _tag_seq
3151

3252
@property
@@ -60,7 +80,12 @@ def add_style_of_type(self, name, style_type, builtin):
6080
*style_type*. `w:style/@customStyle` is set based on the value of
6181
*builtin*.
6282
"""
63-
raise NotImplementedError
83+
style = self.add_style()
84+
style.type = style_type
85+
style.customStyle = None if builtin else True
86+
style.styleId = styleId_from_name(name)
87+
style.name_val = name
88+
return style
6489

6590
def default_for(self, style_type):
6691
"""

tests/oxml/test_styles.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for the docx.oxml.styles module.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import pytest
12+
13+
from docx.enum.style import WD_STYLE_TYPE
14+
15+
from ..unitutil.cxml import element, xml
16+
17+
18+
class DescribeCT_Styles(object):
19+
20+
def it_can_add_a_style_of_type(self, add_fixture):
21+
styles, name, style_type, builtin, expected_xml = add_fixture
22+
style = styles.add_style_of_type(name, style_type, builtin)
23+
assert styles.xml == expected_xml
24+
assert style is styles[-1]
25+
26+
# fixtures -------------------------------------------------------
27+
28+
@pytest.fixture(params=[
29+
('w:styles', 'Foo Bar', WD_STYLE_TYPE.LIST, False,
30+
'w:styles/w:style{w:type=numbering,w:customStyle=1,w:styleId=FooBar'
31+
'}/w:name{w:val=Foo Bar}'),
32+
('w:styles', 'heading 1', WD_STYLE_TYPE.PARAGRAPH, True,
33+
'w:styles/w:style{w:type=paragraph,w:styleId=Heading1}/w:name{w:val'
34+
'=heading 1}'),
35+
])
36+
def add_fixture(self, request):
37+
styles_cxml, name, style_type, builtin, expected_cxml = request.param
38+
styles = element(styles_cxml)
39+
expected_xml = xml(expected_cxml)
40+
return styles, name, style_type, builtin, expected_xml

0 commit comments

Comments
 (0)
X Tutup