Skip to content

Commit 235e2f1

Browse files
author
Steve Canny
committed
doc: refactor _Body.add_paragraph()
* Migrate (text=‘’, style=None) interface for add_paragraph() down to actual implementation so it can be shared when extracted to BlockItemContainer. * Combine unit tests for add_paragraph() in test_api.py
1 parent e192ebc commit 235e2f1

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

docx/api.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ def add_paragraph(self, text='', style=None):
6969
return (``\\r``) characters, each of which is converted to a line
7070
break.
7171
"""
72-
paragraph = self._document_part.add_paragraph()
73-
if text:
74-
paragraph.add_run(text)
75-
if style is not None:
76-
paragraph.style = style
77-
return paragraph
72+
return self._document_part.add_paragraph(text, style)
7873

7974
def add_picture(self, image_path_or_stream, width=None, height=None):
8075
"""

docx/parts/document.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class DocumentPart(XmlPart):
2424
"""
2525
Main document part of a WordprocessingML (WML) package, aka a .docx file.
2626
"""
27-
def add_paragraph(self):
27+
def add_paragraph(self, text='', style=None):
2828
"""
2929
Return a paragraph newly added to the end of body content.
3030
"""
31-
return self.body.add_paragraph()
31+
return self.body.add_paragraph(text, style)
3232

3333
def add_section(self, start_type=WD_SECTION.NEW_PAGE):
3434
"""
@@ -122,12 +122,20 @@ def __init__(self, body_elm, parent):
122122
super(_Body, self).__init__(parent)
123123
self._body = body_elm
124124

125-
def add_paragraph(self):
125+
def add_paragraph(self, text='', style=None):
126126
"""
127-
Return a paragraph newly added to the end of body content.
127+
Return a paragraph newly added to the end of body content, having
128+
*text* in a single run if present, and having paragraph style
129+
*style*. If *style* is |None|, no paragraph style is applied, which
130+
has the same effect as applying the 'Normal' style.
128131
"""
129132
p = self._body.add_p()
130-
return Paragraph(p, self)
133+
paragraph = Paragraph(p, self)
134+
if text:
135+
paragraph.add_run(text)
136+
if style is not None:
137+
paragraph.style = style
138+
return paragraph
131139

132140
def add_table(self, rows, cols):
133141
"""

tests/parts/test_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def it_provides_access_to_the_inline_shapes_in_the_document(
6666
def it_can_add_a_paragraph(self, add_paragraph_fixture):
6767
document_part, body_, p_ = add_paragraph_fixture
6868
p = document_part.add_paragraph()
69-
body_.add_paragraph.assert_called_once_with()
69+
body_.add_paragraph.assert_called_once_with('', None)
7070
assert p is p_
7171

7272
def it_can_add_a_section(self, add_section_fixture):

tests/test_api.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,14 @@ def it_should_raise_on_heading_level_out_of_range(self, document):
6666
with pytest.raises(ValueError):
6767
document.add_heading(level=10)
6868

69-
def it_can_add_an_empty_paragraph(self, add_empty_paragraph_fixture):
70-
document, document_part_, paragraph_ = add_empty_paragraph_fixture
71-
paragraph = document.add_paragraph()
72-
document_part_.add_paragraph.assert_called_once_with()
69+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
70+
document, document_part_, text, style, paragraph_ = (
71+
add_paragraph_fixture
72+
)
73+
paragraph = document.add_paragraph(text, style)
74+
document_part_.add_paragraph.assert_called_once_with(text, style)
7375
assert paragraph is paragraph_
7476

75-
def it_can_add_a_paragraph_of_text(self, add_text_paragraph_fixture):
76-
document, text = add_text_paragraph_fixture
77-
paragraph = document.add_paragraph(text)
78-
paragraph.add_run.assert_called_once_with(text)
79-
80-
def it_can_add_a_styled_paragraph(self, add_styled_paragraph_fixture):
81-
document, style = add_styled_paragraph_fixture
82-
paragraph = document.add_paragraph(style=style)
83-
assert paragraph.style == style
84-
8577
def it_can_add_a_page_break(self, add_page_break_fixture):
8678
document, document_part_, paragraph_, run_ = add_page_break_fixture
8779
paragraph = document.add_page_break()
@@ -177,10 +169,15 @@ def it_creates_styles_part_on_first_access_if_not_present(
177169

178170
# fixtures -------------------------------------------------------
179171

180-
@pytest.fixture
181-
def add_empty_paragraph_fixture(
182-
self, document, document_part_, paragraph_):
183-
return document, document_part_, paragraph_
172+
@pytest.fixture(params=[
173+
('', None),
174+
('', 'Heading1'),
175+
('foo\rbar', 'BodyText'),
176+
])
177+
def add_paragraph_fixture(
178+
self, request, document, document_part_, paragraph_):
179+
text, style = request.param
180+
return document, document_part_, text, style, paragraph_
184181

185182
@pytest.fixture(params=[0, 1, 2, 5, 9])
186183
def add_heading_fixture(
@@ -208,11 +205,6 @@ def add_picture_fixture(self, request, run_, picture_):
208205
def add_section_fixture(self, document, start_type_, section_):
209206
return document, start_type_, section_
210207

211-
@pytest.fixture
212-
def add_styled_paragraph_fixture(self, document):
213-
style = 'foobaresque'
214-
return document, style
215-
216208
@pytest.fixture(params=[None, 'LightShading-Accent1', 'foobar'])
217209
def add_table_fixture(self, request, document, document_part_, table_):
218210
rows, cols = 4, 2
@@ -222,11 +214,6 @@ def add_table_fixture(self, request, document, document_part_, table_):
222214
table_
223215
)
224216

225-
@pytest.fixture
226-
def add_text_paragraph_fixture(self, document):
227-
text = 'foobar\rbarfoo'
228-
return document, text
229-
230217
@pytest.fixture
231218
def init_fixture(self, docx_, open_):
232219
return docx_, open_

0 commit comments

Comments
 (0)