Skip to content

Commit 63f32a4

Browse files
author
Steve Canny
committed
parts: add _Body.add_paragraph()
1 parent aeaf77b commit 63f32a4

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

docx/parts.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from opc import Part
1515

1616
from docx.oxml.base import oxml_fromstring
17+
from docx.text import Paragraph
1718

1819

1920
class _Document(Part):
@@ -43,3 +44,10 @@ class _Body(object):
4344
Proxy for ``<w:body>`` element in this document, having primarily a
4445
container role.
4546
"""
47+
def __init__(self, body_elm):
48+
super(_Body, self).__init__()
49+
self._body = body_elm
50+
51+
def add_paragraph(self):
52+
p = self._body.add_p()
53+
return Paragraph(p)

docx/text.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# text.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-docx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""
11+
Text-related proxy types for python-docx, such as Paragraph and Run.
12+
"""
13+
14+
15+
class Paragraph(object):
16+
"""
17+
Proxy object wrapping ``<w:p>`` element.
18+
"""

tests/test_parts.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
"""Test suite for the docx.parts module."""
1111

12-
from docx.parts import _Document
12+
from docx.parts import _Body, _Document
1313

1414
import pytest
1515

@@ -56,3 +56,22 @@ def it_has_a_body(self, init, _Body_):
5656
# verify -----------------------
5757
_Body_.assert_called_once_with(doc._element.body)
5858
assert body is _Body_.return_value
59+
60+
61+
class Describe_Body(object):
62+
63+
@pytest.fixture
64+
def Paragraph_(self, request):
65+
return class_mock('docx.parts.Paragraph', request)
66+
67+
def it_can_add_a_paragraph_to_itself(self, Paragraph_):
68+
# mockery ----------------------
69+
body_elm = Mock(name='body_elm')
70+
body_elm.add_p.return_value = p_elm = Mock(name='p_elm')
71+
body = _Body(body_elm)
72+
# exercise ---------------------
73+
p = body.add_paragraph()
74+
# verify -----------------------
75+
body_elm.add_p.assert_called_once_with()
76+
Paragraph_.assert_called_once_with(p_elm)
77+
assert p is Paragraph_.return_value

0 commit comments

Comments
 (0)