Skip to content

Commit ea71fc2

Browse files
author
Steve Canny
committed
text: add Paragraph.text
1 parent f13be7b commit ea71fc2

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

docx/text.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ def style(self):
4444
def style(self, style):
4545
self._p.style = None if style == 'Normal' else style
4646

47+
@property
48+
def text(self):
49+
"""
50+
A string formed by concatenating the text of each run in the
51+
paragraph.
52+
"""
53+
text = ''
54+
for run in self.runs:
55+
text += run.text
56+
return text
57+
4758

4859
class Run(object):
4960
"""

features/api-add-paragraph.feature

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@ Feature: Add a paragraph with optional text and style
33
As a programmer using the basic python-docx API
44
I want to add a styled paragraph of text in a single step
55

6-
@wip
76
Scenario: Add an empty paragraph
87
Given a document
98
When I add a paragraph without specifying text or style
109
Then the last paragraph is the empty paragraph I added
1110

12-
@wip
1311
Scenario: Add a paragraph specifying its text
1412
Given a document
15-
When I add a paragraph spefifying its text
13+
When I add a paragraph specifying its text
1614
Then the last paragraph contains the text I specified
1715

18-
@wip
1916
Scenario: Add a paragraph specifying its style
2017
Given a document
21-
When I add a paragraph spefifying its style
22-
Then the style of the last paragraph is the style I specified
18+
When I add a paragraph specifying its style
19+
Then the last paragraph has the style I specified

features/steps/api.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,46 @@
99

1010
# when ====================================================
1111

12+
@when('I add a paragraph specifying its style')
13+
def when_add_paragraph_specifying_style(context):
14+
document = context.document
15+
context.paragraph_style = 'barfoo'
16+
document.add_paragraph(style=context.paragraph_style)
17+
18+
19+
@when('I add a paragraph specifying its text')
20+
def when_add_paragraph_specifying_text(context):
21+
document = context.document
22+
context.paragraph_text = 'foobar'
23+
document.add_paragraph(context.paragraph_text)
24+
25+
1226
@when('I add a paragraph without specifying text or style')
13-
def step_when_add_paragraph_without_specifying_text_or_style(context):
27+
def when_add_paragraph_without_specifying_text_or_style(context):
1428
document = context.document
1529
document.add_paragraph()
1630

1731

1832
# then =====================================================
1933

34+
@then('the last paragraph contains the text I specified')
35+
def then_last_p_contains_specified_text(context):
36+
document = context.document
37+
text = context.paragraph_text
38+
p = document.paragraphs[-1]
39+
assert p.text == text
40+
41+
2042
@then('the last paragraph is the empty paragraph I added')
21-
def step_then_document_contains_text_I_added(context):
43+
def then_last_p_is_empty_paragraph_added(context):
2244
document = context.document
2345
p = document.paragraphs[-1]
2446
assert p.text == ''
47+
48+
49+
@then('the last paragraph has the style I specified')
50+
def then_last_p_has_specified_style(context):
51+
document = context.document
52+
style = context.paragraph_style
53+
p = document.paragraphs[-1]
54+
assert p.style == style

tests/test_text.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from mock import call, create_autospec, Mock
1616

17-
from .oxml.unitdata.text import a_br, a_t, an_r
17+
from .oxml.unitdata.text import a_br, a_t, a_p, an_r
1818
from .unitutil import class_mock
1919

2020

@@ -69,6 +69,24 @@ def it_can_set_its_paragraph_style(self):
6969
p.style = style
7070
assert p_elm.style == expected_setting
7171

72+
def it_knows_the_text_it_contains(self, text_prop_fixture):
73+
p, expected_text = text_prop_fixture
74+
assert p.text == expected_text
75+
76+
# fixtures -------------------------------------------------------
77+
78+
@pytest.fixture
79+
def text_prop_fixture(self):
80+
p = (
81+
a_p().with_nsdecls().with_child(
82+
an_r().with_child(
83+
a_t().with_text('foo'))).with_child(
84+
an_r().with_child(
85+
a_t().with_text(' de bar')))
86+
).element
87+
paragraph = Paragraph(p)
88+
return paragraph, 'foo de bar'
89+
7290

7391
class DescribeRun(object):
7492

0 commit comments

Comments
 (0)