Skip to content

Commit fd31f4d

Browse files
committed
Add support for paragraph highlight color
1 parent 900e880 commit fd31f4d

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ main.py
4545

4646
docs/_build
4747
tests/failures/*.html
48+
49+
~$*.docx

pydocx/export/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ def get_run_styles_to_apply(self, run):
359359
(properties.hidden, self.export_run_property_hidden),
360360
(properties.vertical_align, self.export_run_property_vertical_align),
361361
(properties.color, self.export_run_property_color),
362+
(properties.highlight_color, self.export_run_property_highlight_color),
362363
(not properties.color, self.export_run_property_parent_background_color),
363364
]
364365
for actual_value, handler in property_rules:
@@ -408,6 +409,9 @@ def export_run_property_vertical_align(self, run, results):
408409
def export_run_property_color(self, run, results):
409410
return results
410411

412+
def export_run_property_highlight_color(self, run, results):
413+
return results
414+
411415
def export_run_property_parent_background_color(self, run, results):
412416
return results
413417

pydocx/export/html.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,17 @@ def export_run_property_color(self, run, results):
577577
tag = HtmlTag('span', **attrs)
578578
return self.export_run_property(tag, run, results)
579579

580+
def export_run_property_highlight_color(self, run, results):
581+
if run.properties is None or run.properties.highlight_color is None:
582+
return results
583+
584+
attrs = {
585+
'style': 'background-color: %s' % run.properties.highlight_color
586+
}
587+
tag = HtmlTag('span', **attrs)
588+
589+
return self.export_run_property(tag, run, results)
590+
580591
def export_run_property_parent_background_color(self, run, results):
581592
background_color = None
582593

pydocx/openxml/wordprocessing/run_properties.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ class RunProperties(XmlModel):
2828
sz = XmlChild(name='sz', attrname='val')
2929
clr = XmlChild(name='color', attrname='val')
3030
r_fonts = XmlChild(type=RFonts)
31+
highlight_clr = XmlChild(name='highlight', attrname='val')
3132

3233
@property
3334
def color(self):
3435
if self.clr is None:
3536
return
36-
# TODO: When we support background colors, remove FFFFFF check
37-
if self.clr == '000000' or self.clr == 'FFFFFF':
37+
if self.clr == '000000':
3838
return
3939

4040
return self.clr
4141

42+
@property
43+
def highlight_color(self):
44+
if self.highlight_clr is None:
45+
return
46+
47+
return self.highlight_clr
48+
4249
@property
4350
def position(self):
4451
if self.pos is None:

tests/fixtures/styled_color.docx

-1.41 KB
Binary file not shown.

tests/fixtures/styled_color.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
<p><span style="color:#31849B">BBB</span></p>
33
<table border="1">
44
<tr>
5-
<td style="background-color: #5F497A">CCC</td>
5+
<td style="background-color: #5F497A">
6+
<span style="color:#FFFFFF">CCC</span>
7+
</td>
68
</tr>
79
</table>
8-
<p>DDD</p>
10+
<p>
11+
<span style="background-color: red">
12+
<span style="color:#FFFFFF">DDD</span>
13+
</span>
14+
</p>
15+
<p>
16+
<span style="background-color: yellow">AAA</span>
17+
<span style="background-color: green">BBB</span>
18+
<span style="background-color: blue">CCC</span>
19+
DDD
20+
</p>

0 commit comments

Comments
 (0)