Skip to content

Commit 10e77f0

Browse files
Victor VolleSteve Canny
authored andcommitted
run: add style param to Paragraph.add_run()
1 parent a80f0e3 commit 10e77f0

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

docx/text.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ def __init__(self, p):
5858
super(Paragraph, self).__init__()
5959
self._p = p
6060

61-
def add_run(self, text=None):
61+
def add_run(self, text=None, style=None):
6262
"""
63-
Append a run to this paragraph.
63+
Append a run to this paragraph containing *text* and having character
64+
style identified by style ID *style*.
6465
"""
6566
r = self._p.add_r()
6667
run = Run(r)
6768
if text:
6869
run.add_text(text)
70+
if style:
71+
run.style = style
6972
return run
7073

7174
@property

features/par-add-run.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Feature: Add a run with optional text and style
88
When I add a run specifying its text
99
Then the run contains the text I specified
1010

11-
@wip
1211
Scenario: Add a run specifying its style
1312
Given a paragraph
1413
When I add a run specifying the character style Emphasis

tests/test_text.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def it_has_a_sequence_of_the_runs_it_contains(self, runs_fixture):
3434
assert runs == [run_, run_2_]
3535

3636
def it_can_add_a_run_to_itself(self, add_run_fixture):
37-
paragraph, text, expected_xml = add_run_fixture
38-
run = paragraph.add_run(text)
37+
paragraph, text, style, expected_xml = add_run_fixture
38+
run = paragraph.add_run(text, style)
3939
assert paragraph._p.xml == expected_xml
4040
assert isinstance(run, Run)
4141
assert run._r is paragraph._p.r_lst[0]
@@ -66,21 +66,29 @@ def it_knows_the_text_it_contains(self, text_prop_fixture):
6666

6767
# fixtures -------------------------------------------------------
6868

69-
@pytest.fixture(params=[None, '', 'foobar'])
69+
@pytest.fixture(params=[
70+
(None, None), (None, 'Strong'), ('foobar', None), ('foobar', 'Strong')
71+
])
7072
def add_run_fixture(self, request, paragraph):
71-
text = request.param
73+
text, style = request.param
7274
r_bldr = an_r()
75+
if style:
76+
r_bldr.with_child(
77+
an_rPr().with_child(an_rStyle().with_val(style))
78+
)
7379
if text:
7480
r_bldr.with_child(a_t().with_text(text))
7581
expected_xml = a_p().with_nsdecls().with_child(r_bldr).xml()
76-
return paragraph, text, expected_xml
82+
return paragraph, text, style, expected_xml
83+
84+
# fixture components ---------------------------------------------
7785

7886
@pytest.fixture
7987
def p_(self, request, r_, r_2_):
8088
return instance_mock(request, CT_P, r_lst=(r_, r_2_))
8189

8290
@pytest.fixture
83-
def paragraph(self, request):
91+
def paragraph(self):
8492
p = a_p().with_nsdecls().element
8593
return Paragraph(p)
8694

0 commit comments

Comments
 (0)