Skip to content

Commit 4555d2a

Browse files
author
Steve Canny
committed
doc: small refactoring on Document.add_paragraph()
Document.add_paragraph() can rely on Paragraph.add_run() to add the text.
1 parent cbf3d5c commit 4555d2a

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

docx/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def add_paragraph(self, text='', style=None):
6767
"""
6868
p = self._document_part.add_paragraph()
6969
if text:
70-
r = p.add_run()
71-
r.add_text(text)
70+
p.add_run(text)
7271
if style is not None:
7372
p.style = style
7473
return p

tests/test_api.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ def it_can_add_an_empty_paragraph(self, add_empty_paragraph_fixture):
7070
assert p is p_
7171

7272
def it_can_add_a_paragraph_of_text(self, add_text_paragraph_fixture):
73-
document, text, p_, r_ = add_text_paragraph_fixture
73+
document, text, p_ = add_text_paragraph_fixture
7474
p = document.add_paragraph(text)
75-
p.add_run.assert_called_once_with()
76-
r_.add_text.assert_called_once_with(text)
75+
p.add_run.assert_called_once_with(text)
7776

7877
def it_can_add_a_styled_paragraph(self, add_styled_paragraph_fixture):
7978
document, style, p_ = add_styled_paragraph_fixture
@@ -228,9 +227,9 @@ def add_table_fixture(self, request, document, document_part_, table_):
228227
)
229228

230229
@pytest.fixture
231-
def add_text_paragraph_fixture(self, document, p_, r_):
230+
def add_text_paragraph_fixture(self, document, p_):
232231
text = 'foobar\rbarfoo'
233-
return document, text, p_, r_
232+
return document, text, p_
234233

235234
@pytest.fixture
236235
def init_fixture(self, docx_, open_):

0 commit comments

Comments
 (0)