Skip to content

Commit a51022b

Browse files
author
Steve Canny
committed
dml: add ColorFormat.theme_color setter
1 parent 063396b commit a51022b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

docx/dml/color.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def theme_color(self):
7979
return None
8080
return color.themeColor
8181

82+
@theme_color.setter
83+
def theme_color(self, value):
84+
if value is None:
85+
if self._color is not None:
86+
self._element.rPr._remove_color()
87+
return
88+
self._element.get_or_add_rPr().get_or_add_color().themeColor = value
89+
8290
@property
8391
def type(self):
8492
"""

docx/oxml/text/font.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
Custom element classes related to run properties (font).
55
"""
66

7+
from .. import parse_xml
78
from ...enum.dml import MSO_THEME_COLOR
89
from ...enum.text import WD_UNDERLINE
9-
from ..ns import qn
10+
from ..ns import nsdecls, qn
1011
from ..simpletypes import (
1112
ST_HexColor, ST_HpsMeasure, ST_String, ST_VerticalAlignRun
1213
)
@@ -82,6 +83,13 @@ class CT_RPr(BaseOxmlElement):
8283
oMath = ZeroOrOne('w:oMath', successors=_tag_seq[39:])
8384
del _tag_seq
8485

86+
def _new_color(self):
87+
"""
88+
Override metaclass method to set `w:color/@val` to RGB black on
89+
create.
90+
"""
91+
return parse_xml('<w:color %s w:val="000000"/>' % nsdecls('w'))
92+
8593
@property
8694
def rFonts_ascii(self):
8795
"""

features/txt-font-color.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ Feature: Get and set font color
5656
| a theme | ACCENT_1 |
5757

5858

59-
@wip
6059
Scenario Outline: Set font theme color
6160
Given a font having <type> color
6261
When I assign <value> to font.color.theme_color

tests/dml/test_color.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def it_knows_its_theme_color(self, theme_color_get_fixture):
3636
color_format, expected_value = theme_color_get_fixture
3737
assert color_format.theme_color == expected_value
3838

39+
def it_can_change_its_theme_color(self, theme_color_set_fixture):
40+
color_format, new_value, expected_xml = theme_color_set_fixture
41+
color_format.theme_color = new_value
42+
assert color_format._element.xml == expected_xml
43+
3944
# fixtures ---------------------------------------------
4045

4146
@pytest.fixture(params=[
@@ -87,6 +92,28 @@ def theme_color_get_fixture(self, request):
8792
)
8893
return color_format, expected_value
8994

95+
@pytest.fixture(params=[
96+
('w:r', 'ACCENT_1',
97+
'w:r/w:rPr/w:color{w:val=000000,w:themeColor=accent1}'),
98+
('w:r/w:rPr', 'ACCENT_2',
99+
'w:r/w:rPr/w:color{w:val=000000,w:themeColor=accent2}'),
100+
('w:r/w:rPr/w:color{w:val=101112}', 'ACCENT_3',
101+
'w:r/w:rPr/w:color{w:val=101112,w:themeColor=accent3}'),
102+
('w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}', 'LIGHT_2',
103+
'w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=light2}'),
104+
('w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}', None,
105+
'w:r/w:rPr'),
106+
('w:r', None, 'w:r'),
107+
])
108+
def theme_color_set_fixture(self, request):
109+
r_cxml, member, expected_cxml = request.param
110+
color_format = ColorFormat(element(r_cxml))
111+
new_value = (
112+
None if member is None else getattr(MSO_THEME_COLOR, member)
113+
)
114+
expected_xml = xml(expected_cxml)
115+
return color_format, new_value, expected_xml
116+
90117
@pytest.fixture(params=[
91118
('w:r', None),
92119
('w:r/w:rPr', None),

0 commit comments

Comments
 (0)