Skip to content

Commit 5b25f14

Browse files
author
Steve Canny
committed
style: add DocumentPart.styles
1 parent ea2f8b4 commit 5b25f14

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

docx/parts/document.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def styles(self):
108108
A |Styles| object providing access to the styles in the styles part
109109
of this document.
110110
"""
111-
raise NotImplementedError
111+
return self._styles_part.styles
112112

113113
@property
114114
def tables(self):
@@ -119,6 +119,14 @@ def tables(self):
119119
"""
120120
return self.body.tables
121121

122+
@property
123+
def _styles_part(self):
124+
"""
125+
Instance of |StylesPart| for this document. Creates an empty styles
126+
part if one is not present.
127+
"""
128+
raise NotImplementedError
129+
122130

123131
class _Body(BlockItemContainer):
124132
"""

tests/parts/test_document.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
from docx.package import ImageParts, Package
1616
from docx.parts.document import _Body, DocumentPart, InlineShapes, Sections
1717
from docx.parts.image import ImagePart
18+
from docx.parts.styles import StylesPart
1819
from docx.section import Section
1920
from docx.shape import InlineShape
21+
from docx.styles.styles import Styles
2022
from docx.table import Table
2123
from docx.text.paragraph import Paragraph
2224
from docx.text.run import Run
@@ -54,6 +56,11 @@ def it_provides_access_to_the_document_tables(self, tables_fixture):
5456
tables = document_part.tables
5557
assert tables is tables_
5658

59+
def it_provides_access_to_the_document_styles(self, styles_fixture):
60+
document_part, styles_ = styles_fixture
61+
styles = document_part.styles
62+
assert styles is styles_
63+
5764
def it_provides_access_to_the_inline_shapes_in_the_document(
5865
self, inline_shapes_fixture):
5966
document, InlineShapes_, body_elm = inline_shapes_fixture
@@ -162,11 +169,18 @@ def paragraphs_fixture(self, document_part_body_, body_, paragraphs_):
162169
return document_part, paragraphs_
163170

164171
@pytest.fixture
165-
def sections_fixture(self, request, Sections_):
172+
def sections_fixture(self, Sections_):
166173
document_elm = a_document().with_nsdecls().element
167174
document = DocumentPart(None, None, document_elm, None)
168175
return document, document_elm, Sections_
169176

177+
@pytest.fixture
178+
def styles_fixture(self, _styles_part_prop_, styles_part_, styles_):
179+
document_part = DocumentPart(None, None, None, None)
180+
_styles_part_prop_.return_value = styles_part_
181+
styles_part_.styles = styles_
182+
return document_part, styles_
183+
170184
@pytest.fixture
171185
def tables_fixture(self, document_part_body_, body_, tables_):
172186
document_part = DocumentPart(None, None, None, None)
@@ -275,6 +289,18 @@ def sectPr_(self, request):
275289
def start_type_(self, request):
276290
return instance_mock(request, int)
277291

292+
@pytest.fixture
293+
def styles_(self, request):
294+
return instance_mock(request, Styles)
295+
296+
@pytest.fixture
297+
def styles_part_(self, request):
298+
return instance_mock(request, StylesPart)
299+
300+
@pytest.fixture
301+
def _styles_part_prop_(self, request):
302+
return property_mock(request, DocumentPart, '_styles_part')
303+
278304
@pytest.fixture
279305
def table_(self, request):
280306
return instance_mock(request, Table)

0 commit comments

Comments
 (0)