Skip to content

Commit 86ae5d2

Browse files
author
Steve Canny
committed
text: add Run.italic setter
1 parent 2f2d0b4 commit 86ae5d2

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

docx/oxml/text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ def add_b(self):
260260
self.insert(0, b)
261261
return b
262262

263+
def add_i(self):
264+
"""
265+
Return a newly added <w:i/> child element.
266+
"""
267+
i = OxmlElement('w:i')
268+
self.insert(0, i)
269+
return i
270+
263271
@property
264272
def b(self):
265273
"""
@@ -286,6 +294,11 @@ def remove_b(self):
286294
for b in b_lst:
287295
self.remove(b)
288296

297+
def remove_i(self):
298+
i_lst = self.findall(qn('w:i'))
299+
for i in i_lst:
300+
self.remove(i)
301+
289302

290303
class CT_Text(OxmlBaseElement):
291304
"""

docx/text.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ def italic(self):
132132
return None
133133
return i.val
134134

135+
@italic.setter
136+
def italic(self, value):
137+
rPr = self._r.get_or_add_rPr()
138+
rPr.remove_i()
139+
if value is not None:
140+
i = rPr.add_i()
141+
if bool(value) is False:
142+
i.val = False
143+
135144
@property
136145
def text(self):
137146
"""

features/run-bold-italic.feature

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ Feature: Apply bold or italic to run
1818
When I assign False to its bold property
1919
Then the run appears without bold regardless of its style hierarchy
2020

21-
@wip
2221
Scenario: Apply italic to a run
2322
Given a run
2423
When I assign True to its italic property
2524
Then the run appears in italic typeface
2625

27-
@wip
2826
Scenario: Remove italic from a run
2927
Given a run having italic set on
3028
When I assign None to its italic property
3129
Then the run appears with its inherited italic setting
3230

33-
@wip
3431
Scenario: Set italic off unconditionally
3532
Given a run
3633
When I assign False to its italic property

tests/test_text.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ def it_can_change_its_bold_setting(self, bold_set_fixture):
103103
run.bold = bold_value
104104
assert run._r.xml == expected_xml
105105

106+
def it_can_change_its_italic_setting(self, italic_set_fixture):
107+
run, italic_value, expected_xml = italic_set_fixture
108+
run.italic = italic_value
109+
assert run._r.xml == expected_xml
110+
106111
def it_can_add_text(self, add_text_fixture):
107112
run, text_str, expected_xml, Text_ = add_text_fixture
108113
_text = run.add_text(text_str)
@@ -195,6 +200,23 @@ def bold_set_fixture(self, request):
195200
expected_xml = an_r().with_nsdecls().with_child(rPr_bldr).xml()
196201
return run, bold_value, expected_xml
197202

203+
@pytest.fixture(params=[True, False, None])
204+
def italic_set_fixture(self, request):
205+
# run --------------------------
206+
r = an_r().with_nsdecls().element
207+
run = Run(r)
208+
# italic_value -------------------
209+
italic_value = request.param
210+
# expected_xml -----------------
211+
rPr_bldr = an_rPr()
212+
if italic_value is not None:
213+
i_bldr = an_i()
214+
if italic_value is False:
215+
i_bldr.with_val(0)
216+
rPr_bldr.with_child(i_bldr)
217+
expected_xml = an_r().with_nsdecls().with_child(rPr_bldr).xml()
218+
return run, italic_value, expected_xml
219+
198220
@pytest.fixture
199221
def run(self):
200222
r = an_r().with_nsdecls().element

0 commit comments

Comments
 (0)