Skip to content

Commit 4f98312

Browse files
author
Steve Canny
committed
doc: add Document.add_paragraph()
1 parent fb5e7d7 commit 4f98312

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

docx/document.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ def __init__(self, element, part):
2424
self._part = part
2525
self.__body = None
2626

27+
def add_paragraph(self, text='', style=None):
28+
"""
29+
Return a paragraph newly added to the end of the document, populated
30+
with *text* and having paragraph style *style*. *text* can contain
31+
tab (``\\t``) characters, which are converted to the appropriate XML
32+
form for a tab. *text* can also include newline (``\\n``) or carriage
33+
return (``\\r``) characters, each of which is converted to a line
34+
break.
35+
"""
36+
return self._body.add_paragraph(text, style)
37+
2738
@property
2839
def part(self):
2940
"""

tests/test_document.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212

1313
from docx.document import _Body, Document
1414
from docx.parts.document import DocumentPart
15+
from docx.text.paragraph import Paragraph
1516

1617
from .unitutil.cxml import element
17-
from .unitutil.mock import class_mock, instance_mock
18+
from .unitutil.mock import class_mock, instance_mock, property_mock
1819

1920

2021
class DescribeDocument(object):
2122

23+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
24+
document, text, style, paragraph_ = add_paragraph_fixture
25+
paragraph = document.add_paragraph(text, style)
26+
document._body.add_paragraph.assert_called_once_with(text, style)
27+
assert paragraph is paragraph_
28+
2229
def it_provides_access_to_the_document_part(self, part_fixture):
2330
document, part_ = part_fixture
2431
assert document.part is part_
@@ -31,6 +38,17 @@ def it_provides_access_to_the_document_body(self, body_fixture):
3138

3239
# fixtures -------------------------------------------------------
3340

41+
@pytest.fixture(params=[
42+
('', None),
43+
('', 'Heading 1'),
44+
('foo\rbar', 'Body Text'),
45+
])
46+
def add_paragraph_fixture(self, request, body_prop_, paragraph_):
47+
text, style = request.param
48+
document = Document(None, None)
49+
body_prop_.return_value.add_paragraph.return_value = paragraph_
50+
return document, text, style, paragraph_
51+
3452
@pytest.fixture
3553
def body_fixture(self, _Body_, body_):
3654
document_elm = element('w:document/w:body')
@@ -53,6 +71,14 @@ def _Body_(self, request, body_):
5371
def body_(self, request):
5472
return instance_mock(request, _Body)
5573

74+
@pytest.fixture
75+
def body_prop_(self, request):
76+
return property_mock(request, Document, '_body')
77+
5678
@pytest.fixture
5779
def document_part_(self, request):
5880
return instance_mock(request, DocumentPart)
81+
82+
@pytest.fixture
83+
def paragraph_(self, request):
84+
return instance_mock(request, Paragraph)

0 commit comments

Comments
 (0)