Skip to content

Commit e2d6311

Browse files
author
Steve Canny
committed
blkct: add BlockItemContainer.add_paragraph()
* remove _Body.add_paragraph(), inheriting that method from BlockItemContainer
1 parent 72b3d5d commit e2d6311

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

docx/blkcntnr.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import absolute_import, print_function
1010

1111
from .shared import Parented
12+
from .text import Paragraph
1213

1314

1415
class BlockItemContainer(Parented):
@@ -21,3 +22,18 @@ class BlockItemContainer(Parented):
2122
def __init__(self, element, parent):
2223
super(BlockItemContainer, self).__init__(parent)
2324
self._element = element
25+
26+
def add_paragraph(self, text='', style=None):
27+
"""
28+
Return a paragraph newly added to the end of the content in this
29+
container, having *text* in a single run if present, and having
30+
paragraph style *style*. If *style* is |None|, no paragraph style is
31+
applied, which has the same effect as applying the 'Normal' style.
32+
"""
33+
p = self._element.add_p()
34+
paragraph = Paragraph(p, self)
35+
if text:
36+
paragraph.add_run(text)
37+
if style is not None:
38+
paragraph.style = style
39+
return paragraph

docx/parts/document.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,6 @@ def __init__(self, body_elm, parent):
123123
super(_Body, self).__init__(body_elm, parent)
124124
self._body = body_elm
125125

126-
def add_paragraph(self, text='', style=None):
127-
"""
128-
Return a paragraph newly added to the end of body content, having
129-
*text* in a single run if present, and having paragraph style
130-
*style*. If *style* is |None|, no paragraph style is applied, which
131-
has the same effect as applying the 'Normal' style.
132-
"""
133-
p = self._body.add_p()
134-
paragraph = Paragraph(p, self)
135-
if text:
136-
paragraph.add_run(text)
137-
if style is not None:
138-
paragraph.style = style
139-
return paragraph
140-
141126
def add_table(self, rows, cols):
142127
"""
143128
Return a table having *rows* rows and *cols* cols, newly appended to

tests/test_blkcntnr.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for the docx.blkcntnr (block item container) module
5+
"""
6+
7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
import pytest
10+
11+
from docx.blkcntnr import BlockItemContainer
12+
from docx.text import Paragraph
13+
14+
from .unitutil.cxml import element, xml
15+
16+
17+
class DescribeBlockItemContainer(object):
18+
19+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
20+
blkcntnr, text, style, expected_xml = add_paragraph_fixture
21+
paragraph = blkcntnr.add_paragraph(text, style)
22+
assert blkcntnr._element.xml == expected_xml
23+
assert isinstance(paragraph, Paragraph)
24+
25+
# fixtures -------------------------------------------------------
26+
27+
@pytest.fixture(params=[
28+
('w:body', '', None,
29+
'w:body/w:p'),
30+
('w:body', 'foobar', None,
31+
'w:body/w:p/w:r/w:t"foobar"'),
32+
('w:body', '', 'Heading1',
33+
'w:body/w:p/w:pPr/w:pStyle{w:val=Heading1}'),
34+
('w:body', 'barfoo', 'BodyText',
35+
'w:body/w:p/(w:pPr/w:pStyle{w:val=BodyText},w:r/w:t"barfoo")'),
36+
])
37+
def add_paragraph_fixture(self, request):
38+
blkcntnr_cxml, text, style, after_cxml = request.param
39+
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
40+
expected_xml = xml(after_cxml)
41+
return blkcntnr, text, style, expected_xml

0 commit comments

Comments
 (0)