Skip to content

Commit 2d909ca

Browse files
author
Steve Canny
committed
doc: add DocumentPart.get_style_id()
1 parent 4f3bcc7 commit 2d909ca

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docx/parts/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get_style_id(self, style_or_name, style_type):
8282
if *style_or_name* is a style of the wrong type or names a style not
8383
present in the document.
8484
"""
85-
raise NotImplementedError
85+
return self.styles.get_style_id(style_or_name, style_type)
8686

8787
@lazyproperty
8888
def inline_shapes(self):

docx/styles/styles.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ def get_by_id(self, style_id, style_type):
5858
return self.default(style_type)
5959
return self._get_by_id(style_id, style_type)
6060

61+
def get_style_id(self, style_or_name, style_type):
62+
"""
63+
Return the id of the style corresponding to *style_or_name*, or
64+
|None| if *style_or_name* is |None|. If *style_or_name* is not
65+
a style object, the style is looked up using *style_or_name* as
66+
a style name, raising |ValueError| if no style with that name is
67+
defined. Raises |ValueError| if the target style is not of
68+
*style_type*.
69+
"""
70+
raise NotImplementedError
71+
6172
def _get_by_id(self, style_id, style_type):
6273
"""
6374
Return the style of *style_type* matching *style_id*. Returns the

tests/parts/test_document.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ def it_can_get_a_style_by_id(self, get_style_fixture):
114114
)
115115
assert style is style_
116116

117+
def it_can_get_the_id_of_a_style(self, get_style_id_fixture):
118+
document_part, style_or_name, style_type, style_id_ = (
119+
get_style_id_fixture
120+
)
121+
style_id = document_part.get_style_id(style_or_name, style_type)
122+
123+
document_part.styles.get_style_id.assert_called_once_with(
124+
style_or_name, style_type
125+
)
126+
assert style_id is style_id_
127+
117128
def it_provides_access_to_its_styles_part_to_help(
118129
self, styles_part_get_fixture):
119130
document_part, styles_part_ = styles_part_get_fixture
@@ -171,6 +182,13 @@ def get_style_fixture(self, styles_prop_, style_):
171182
styles_prop_.return_value.get_by_id.return_value = style_
172183
return document_part, style_id, style_type, style_
173184

185+
@pytest.fixture
186+
def get_style_id_fixture(self, styles_prop_):
187+
document_part = DocumentPart(None, None, None, None)
188+
style_or_name, style_type, style_id_ = 'Foo Bar', 1, 'FooBar'
189+
styles_prop_.return_value.get_style_id.return_value = style_id_
190+
return document_part, style_or_name, style_type, style_id_
191+
174192
@pytest.fixture
175193
def inline_shapes_fixture(self, request, InlineShapes_):
176194
document_elm = (

0 commit comments

Comments
 (0)