Skip to content

Commit 884e4f1

Browse files
author
Steve Canny
committed
style: add _Styles._translate_special_case_names()
* elaborate unit test to exercise new helper method * elaborate cxml grammar to allow spaces in attribute values
1 parent d096821 commit 884e4f1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

docx/styles/styles.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __getitem__(self, key):
2525
"""
2626
Enables dictionary-style access by style id or UI name.
2727
"""
28+
key = self._translate_special_case_names(key)
2829
for get in (self._element.get_by_id, self._element.get_by_name):
2930
style_elm = get(key)
3031
if style_elm is not None:
@@ -36,3 +37,24 @@ def __iter__(self):
3637

3738
def __len__(self):
3839
return len(self._element.style_lst)
40+
41+
@staticmethod
42+
def _translate_special_case_names(name):
43+
"""
44+
Translate special-case style names from their English UI
45+
counterparts. Some style names are stored differently than they
46+
appear in the UI, with a leading lowercase letter, perhaps for legacy
47+
reasons.
48+
"""
49+
return {
50+
'Caption': 'caption',
51+
'Heading 1': 'heading 1',
52+
'Heading 2': 'heading 2',
53+
'Heading 3': 'heading 3',
54+
'Heading 4': 'heading 4',
55+
'Heading 5': 'heading 5',
56+
'Heading 6': 'heading 6',
57+
'Heading 7': 'heading 7',
58+
'Heading 8': 'heading 8',
59+
'Heading 9': 'heading 9',
60+
}.get(name, name)

tests/styles/test_styles.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ def get_by_id_fixture(self, request):
6464
return styles, 'Foobar', expected_element
6565

6666
@pytest.fixture(params=[
67-
('w:styles/(w:style%s/w:name{w:val=foo},w:style,w:style)', 0),
68-
('w:styles/(w:style,w:style%s/w:name{w:val=foo},w:style)', 1),
69-
('w:styles/(w:style,w:style,w:style%s/w:name{w:val=foo})', 2),
67+
('w:styles/(w:style%s/w:name{w:val=foo},w:style)', 'foo', 0),
68+
('w:styles/(w:style,w:style%s/w:name{w:val=foo})', 'foo', 1),
69+
('w:styles/w:style%s/w:name{w:val=heading 1}', 'Heading 1', 0),
7070
])
7171
def get_by_name_fixture(self, request):
72-
styles_cxml_tmpl, style_idx = request.param
72+
styles_cxml_tmpl, key, style_idx = request.param
7373
styles_cxml = styles_cxml_tmpl % '{w:type=character}'
7474
styles = Styles(element(styles_cxml))
7575
expected_element = styles._element[style_idx]
76-
return styles, 'foo', expected_element
76+
return styles, key, expected_element
7777

7878
@pytest.fixture(params=[
7979
('w:styles/(w:style,w:style/w:name{w:val=foo},w:style)'),

tests/unitutil/cxml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def grammar():
228228

229229
# np:attr_name=attr_val ----------------------
230230
attr_name = Word(alphas + ':')
231-
attr_val = Word(alphanums + '-.%')
231+
attr_val = Word(alphanums + ' -.%')
232232
attr_def = Group(attr_name + equal + attr_val)
233233
attr_list = open_brace + delimitedList(attr_def) + close_brace
234234

0 commit comments

Comments
 (0)