Skip to content

Commit c0ea333

Browse files
author
Steve Canny
committed
doc: add DocumentPart.get_style()
1 parent c95b480 commit c0ea333

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docx/parts/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_style(self, style_id, style_type):
7272
default style for *style_type* if *style_id* is |None| or does not
7373
match a defined style of *style_type*.
7474
"""
75-
raise NotImplementedError
75+
return self.styles.get_by_id(style_id, style_type)
7676

7777
@lazyproperty
7878
def inline_shapes(self):

docx/styles/styles.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def __iter__(self):
3838
def __len__(self):
3939
return len(self._element.style_lst)
4040

41+
def get_by_id(self, style_id, style_type):
42+
"""
43+
Return the style of *style_type* matching *style_id*. Returns the
44+
default for *style_type* if *style_id* is not found or is |None|, or
45+
if the style having *style_id* is not of *style_type*.
46+
"""
47+
raise NotImplementedError
48+
4149
@staticmethod
4250
def _translate_special_case_names(name):
4351
"""

tests/parts/test_document.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from docx.parts.styles import StylesPart
1919
from docx.section import Section
2020
from docx.shape import InlineShape
21+
from docx.styles.style import BaseStyle
2122
from docx.styles.styles import Styles
2223
from docx.table import Table
2324
from docx.text.paragraph import Paragraph
@@ -105,6 +106,14 @@ def it_knows_the_next_available_xml_id(self, next_id_fixture):
105106
document, expected_id = next_id_fixture
106107
assert document.next_id == expected_id
107108

109+
def it_can_get_a_style_by_id(self, get_style_fixture):
110+
document_part, style_id, style_type, style_ = get_style_fixture
111+
style = document_part.get_style(style_id, style_type)
112+
document_part.styles.get_by_id.assert_called_once_with(
113+
style_id, style_type
114+
)
115+
assert style is style_
116+
108117
def it_provides_access_to_its_styles_part_to_help(
109118
self, styles_part_get_fixture):
110119
document_part, styles_part_ = styles_part_get_fixture
@@ -146,7 +155,7 @@ def add_table_fixture(self, document_part_body_, body_, table_):
146155
return document_part, rows, cols, body_, table_
147156

148157
@pytest.fixture
149-
def body_fixture(self, request, _Body_):
158+
def body_fixture(self, _Body_):
150159
document_elm = (
151160
a_document().with_nsdecls().with_child(
152161
a_body())
@@ -155,6 +164,13 @@ def body_fixture(self, request, _Body_):
155164
document_part = DocumentPart(None, None, document_elm, None)
156165
return document_part, _Body_, body_elm
157166

167+
@pytest.fixture
168+
def get_style_fixture(self, styles_prop_, style_):
169+
document_part = DocumentPart(None, None, None, None)
170+
style_id, style_type = 'Foobar', 1
171+
styles_prop_.return_value.get_by_id.return_value = style_
172+
return document_part, style_id, style_type, style_
173+
158174
@pytest.fixture
159175
def inline_shapes_fixture(self, request, InlineShapes_):
160176
document_elm = (
@@ -325,6 +341,10 @@ def sectPr_(self, request):
325341
def start_type_(self, request):
326342
return instance_mock(request, int)
327343

344+
@pytest.fixture
345+
def style_(self, request):
346+
return instance_mock(request, BaseStyle)
347+
328348
@pytest.fixture
329349
def styles_(self, request):
330350
return instance_mock(request, Styles)
@@ -337,6 +357,12 @@ def StylesPart_(self, request):
337357
def styles_part_(self, request):
338358
return instance_mock(request, StylesPart)
339359

360+
@pytest.fixture
361+
def styles_prop_(self, request, styles_):
362+
return property_mock(
363+
request, DocumentPart, 'styles', return_value=styles_
364+
)
365+
340366
@pytest.fixture
341367
def _styles_part_prop_(self, request):
342368
return property_mock(request, DocumentPart, '_styles_part')

0 commit comments

Comments
 (0)