Skip to content

Commit ae1c8d1

Browse files
author
Steve Canny
committed
api: add Document.paragraphs
1 parent 8cdfa29 commit ae1c8d1

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

docx/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def inline_shapes(self):
6464
"""
6565
return self._document_part.inline_shapes
6666

67+
@property
68+
def paragraphs(self):
69+
"""
70+
A list of |Paragraph| instances corresponding to the paragraphs in
71+
the document, in document order. Note that paragraphs within revision
72+
marks such as inserted or deleted do not appear in this list.
73+
"""
74+
return self._document_part.paragraphs
75+
6776
def save(self, path_or_stream):
6877
"""
6978
Save this document to *path_or_stream*, which can be either a path to

docx/parts/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ def next_id(self):
8585
if n not in used_ids:
8686
return n
8787

88+
@property
89+
def paragraphs(self):
90+
"""
91+
A list of |Paragraph| instances corresponding to the paragraphs in
92+
the document, in document order. Note that paragraphs within revision
93+
marks such as inserted or deleted do not appear in this list.
94+
"""
95+
# return self.body.paragraphs
96+
raise NotImplementedError
97+
8898
@property
8999
def part(self):
90100
"""

tests/test_api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def it_provides_access_to_the_document_inline_shapes(self, document):
6565
body = document.inline_shapes
6666
assert body is document._document_part.inline_shapes
6767

68+
def it_provides_access_to_the_document_paragraphs(
69+
self, paragraphs_fixture):
70+
document, paragraphs_ = paragraphs_fixture
71+
paragraphs = document.paragraphs
72+
assert paragraphs is paragraphs_
73+
6874
def it_can_add_an_inline_picture(self, add_picture_fixture):
6975
document, inline_shapes, image_path_or_stream_, inline_picture_ = (
7076
add_picture_fixture
@@ -114,11 +120,12 @@ def document(self, open_):
114120
return Document()
115121

116122
@pytest.fixture
117-
def document_part_(self, request, p_):
123+
def document_part_(self, request, p_, paragraphs_):
118124
document_part_ = instance_mock(
119125
request, DocumentPart, content_type=CT.WML_DOCUMENT_MAIN
120126
)
121127
document_part_.add_paragraph.return_value = p_
128+
document_part_.paragraphs = paragraphs_
122129
return document_part_
123130

124131
@pytest.fixture
@@ -158,6 +165,14 @@ def package_(self, request, document_part_):
158165
package_.main_document = document_part_
159166
return package_
160167

168+
@pytest.fixture
169+
def paragraphs_(self, request):
170+
return instance_mock(request, list)
171+
172+
@pytest.fixture
173+
def paragraphs_fixture(self, document, paragraphs_):
174+
return document, paragraphs_
175+
161176
@pytest.fixture
162177
def r_(self, request):
163178
return instance_mock(request, Run)

0 commit comments

Comments
 (0)