Skip to content

Commit 17dce95

Browse files
DKWoodsSteve Canny
authored and
Steve Canny
committed
font: add Font.highlight_color setter
1 parent 85cd8bf commit 17dce95

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

docx/oxml/text/font.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ def highlight_val(self):
109109
return None
110110
return highlight.val
111111

112+
@highlight_val.setter
113+
def highlight_val(self, value):
114+
if value is None:
115+
self._remove_highlight()
116+
return
117+
highlight = self.get_or_add_highlight()
118+
highlight.val = value
119+
112120
@property
113121
def rFonts_ascii(self):
114122
"""

docx/text/font.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ def highlight_color(self):
136136
return None
137137
return rPr.highlight_val
138138

139+
@highlight_color.setter
140+
def highlight_color(self, value):
141+
rPr = self._element.get_or_add_rPr()
142+
rPr.highlight_val = value
143+
139144
@property
140145
def italic(self):
141146
"""

features/txt-font-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Get or set font properties
1515
| bright green | BRIGHT_GREEN |
1616

1717

18-
@wip
1918
Scenario Outline: Set highlight color
2019
Given a font having <color> highlighting
2120
When I assign <value> to font.highlight_color

tests/text/test_font.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def it_knows_its_highlight_color(self, highlight_get_fixture):
8585
font, expected_value = highlight_get_fixture
8686
assert font.highlight_color is expected_value
8787

88+
def it_can_change_its_highlight_color(self, highlight_set_fixture):
89+
font, highlight_color, expected_xml = highlight_set_fixture
90+
font.highlight_color = highlight_color
91+
assert font._element.xml == expected_xml
92+
8893
# fixtures -------------------------------------------------------
8994

9095
@pytest.fixture(params=[
@@ -189,6 +194,26 @@ def highlight_get_fixture(self, request):
189194
font = Font(element(r_cxml), None)
190195
return font, expected_value
191196

197+
@pytest.fixture(params=[
198+
('w:r', WD_COLOR.AUTO,
199+
'w:r/w:rPr/w:highlight{w:val=default}'),
200+
('w:r/w:rPr', WD_COLOR.BRIGHT_GREEN,
201+
'w:r/w:rPr/w:highlight{w:val=green}'),
202+
('w:r/w:rPr/w:highlight{w:val=green}', WD_COLOR.YELLOW,
203+
'w:r/w:rPr/w:highlight{w:val=yellow}'),
204+
('w:r/w:rPr/w:highlight{w:val=yellow}', None,
205+
'w:r/w:rPr'),
206+
('w:r/w:rPr', None,
207+
'w:r/w:rPr'),
208+
('w:r', None,
209+
'w:r/w:rPr'),
210+
])
211+
def highlight_set_fixture(self, request):
212+
r_cxml, value, expected_cxml = request.param
213+
font = Font(element(r_cxml), None)
214+
expected_xml = xml(expected_cxml)
215+
return font, value, expected_xml
216+
192217
@pytest.fixture(params=[
193218
('w:r', None),
194219
('w:r/w:rPr', None),

0 commit comments

Comments
 (0)