Skip to content

Commit 6b65381

Browse files
author
Steve Canny
committed
text: add 'text' param to Paragraph.add_run()
1 parent 5d2afe4 commit 6b65381

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

docx/text.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ def __init__(self, p):
1717
super(Paragraph, self).__init__()
1818
self._p = p
1919

20-
def add_run(self):
20+
def add_run(self, text=None):
2121
"""
2222
Append a run to this paragraph.
2323
"""
2424
r = self._p.add_r()
25-
return Run(r)
25+
run = Run(r)
26+
if text:
27+
run.add_text(text)
28+
return run
2629

2730
@property
2831
def runs(self):

tests/test_text.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def it_has_a_sequence_of_the_runs_it_contains(self, runs_fixture):
2929
assert runs == [run_, run_2_]
3030

3131
def it_can_add_a_run_to_itself(self, add_run_fixture):
32-
paragraph, expected_xml = add_run_fixture
33-
run = paragraph.add_run()
32+
paragraph, text, expected_xml = add_run_fixture
33+
if text is None:
34+
run = paragraph.add_run()
35+
else:
36+
run = paragraph.add_run(text)
3437
assert paragraph._p.xml == expected_xml
3538
assert isinstance(run, Run)
3639
assert run._r is paragraph._p.r_lst[0]
@@ -61,10 +64,14 @@ def it_knows_the_text_it_contains(self, text_prop_fixture):
6164

6265
# fixtures -------------------------------------------------------
6366

64-
@pytest.fixture
65-
def add_run_fixture(self, paragraph):
66-
expected_xml = a_p().with_nsdecls().with_child(an_r()).xml()
67-
return paragraph, expected_xml
67+
@pytest.fixture(params=[None, '', 'foobar'])
68+
def add_run_fixture(self, request, paragraph):
69+
text = request.param
70+
r_bldr = an_r()
71+
if text:
72+
r_bldr.with_child(a_t().with_text(text))
73+
expected_xml = a_p().with_nsdecls().with_child(r_bldr).xml()
74+
return paragraph, text, expected_xml
6875

6976
@pytest.fixture
7077
def p_(self, request, r_, r_2_):

0 commit comments

Comments
 (0)