Skip to content

Commit 8995fe3

Browse files
author
Steve Canny
committed
style: add BaseSty._translate_special_case_names()
* update api-add-heading feature to new style API and remove from WIP
1 parent 7eb8d66 commit 8995fe3

4 files changed

Lines changed: 43 additions & 19 deletions

File tree

docx/styles/style.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def name(self):
4040
"""
4141
The UI name of this style.
4242
"""
43-
return self._element.name_val
43+
name = self._element.name_val
44+
if name is not None:
45+
return self._translate_special_case_names(name)
46+
return name
4447

4548
@name.setter
4649
def name(self, value):
@@ -68,6 +71,26 @@ def type(self):
6871
return WD_STYLE_TYPE.PARAGRAPH
6972
return type
7073

74+
@staticmethod
75+
def _translate_special_case_names(name):
76+
"""
77+
Translate special-case style names to their English UI counterparts.
78+
Some style names are stored differently than they appear in the UI,
79+
with a leading lowercase letter, perhaps for legacy reasons.
80+
"""
81+
return {
82+
'caption': 'Caption',
83+
'heading 1': 'Heading 1',
84+
'heading 2': 'Heading 2',
85+
'heading 3': 'Heading 3',
86+
'heading 4': 'Heading 4',
87+
'heading 5': 'Heading 5',
88+
'heading 6': 'Heading 6',
89+
'heading 7': 'Heading 7',
90+
'heading 8': 'Heading 8',
91+
'heading 9': 'Heading 9',
92+
}.get(name, name)
93+
7194

7295
class _CharacterStyle(BaseStyle):
7396
"""

features/api-add-heading.feature

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ Feature: Add a section heading with text
33
As a programmer using the basic python-docx API
44
I need a method to add a heading with its text in a single step
55

6-
@wip
76
Scenario: Add a heading specifying only its text
87
Given a document
98
When I add a heading specifying only its text
10-
Then the style of the last paragraph is 'Heading1'
9+
Then the style of the last paragraph is 'Heading 1'
1110
And the last paragraph contains the heading text
1211

13-
@wip
1412
Scenario Outline: Add a heading specifying level
1513
Given a document
1614
When I add a heading specifying level=<heading level>
@@ -19,12 +17,12 @@ Feature: Add a section heading with text
1917
Examples: Heading level styles
2018
| heading level | paragraph style |
2119
| 0 | Title |
22-
| 1 | Heading1 |
23-
| 2 | Heading2 |
24-
| 3 | Heading3 |
25-
| 4 | Heading4 |
26-
| 5 | Heading5 |
27-
| 6 | Heading6 |
28-
| 7 | Heading7 |
29-
| 8 | Heading8 |
30-
| 9 | Heading9 |
20+
| 1 | Heading 1 |
21+
| 2 | Heading 2 |
22+
| 3 | Heading 3 |
23+
| 4 | Heading 4 |
24+
| 5 | Heading 5 |
25+
| 6 | Heading 6 |
26+
| 7 | Heading 7 |
27+
| 8 | Heading 8 |
28+
| 9 | Heading 9 |

features/steps/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ def then_last_p_is_empty_paragraph_added(context):
152152
assert p.text == ''
153153

154154

155-
@then('the style of the last paragraph is \'{style}\'')
156-
def then_style_of_last_paragraph_is_style(context, style):
155+
@then('the style of the last paragraph is \'{style_name}\'')
156+
def then_the_style_of_the_last_paragraph_is_style(context, style_name):
157157
document = context.document
158-
p = document.paragraphs[-1]
159-
assert p.style == style
158+
paragraph = document.paragraphs[-1]
159+
assert paragraph.style.name == style_name, (
160+
'got %s' % paragraph.style.name
161+
)
160162

161163

162164
@then('the table style is \'{style}\'')

tests/styles/test_style.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def id_set_fixture(self, request):
141141
return style, new_value, expected_xml
142142

143143
@pytest.fixture(params=[
144-
('w:style{w:type=table}', None),
145-
('w:style{w:type=table}/w:name{w:val=Boofar}', 'Boofar'),
144+
('w:style{w:type=table}', None),
145+
('w:style{w:type=table}/w:name{w:val=Boofar}', 'Boofar'),
146+
('w:style{w:type=table}/w:name{w:val=heading 1}', 'Heading 1'),
146147
])
147148
def name_get_fixture(self, request):
148149
style_cxml, expected_value = request.param

0 commit comments

Comments
 (0)