Skip to content

Commit ce390d3

Browse files
committed
fix: map highlight w:val="none" to None instead of raising
`w:val="none"` is a valid `ST_HighlightColor` value that Word writes when a highlight is explicitly cleared from a run. It has no `WD_COLOR_INDEX` member, so reading `Font.highlight_color` on such a run raised `ValueError: WD_COLOR_INDEX has no XML mapping for 'none'` from `BaseXmlEnum.from_xml()`, which aborts parsing of any document containing the value. `none` semantically means "not highlighted", which python-docx already represents as `None` (the value returned when no `w:highlight` element is present), and which the `highlight_val` setter writes by removing the element. Map the explicit `none` value to `None` in the getter to match. `none` is the only `ST_HighlightColor` value absent from `WD_COLOR_INDEX`, so this closes the gap completely.
1 parent e454546 commit ce390d3

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/docx/oxml/text/font.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from docx.enum.dml import MSO_THEME_COLOR
1010
from docx.enum.text import WD_COLOR_INDEX, WD_UNDERLINE
11-
from docx.oxml.ns import nsdecls
11+
from docx.oxml.ns import nsdecls, qn
1212
from docx.oxml.parser import parse_xml
1313
from docx.oxml.simpletypes import (
1414
ST_HexColor,
@@ -159,6 +159,12 @@ def highlight_val(self) -> WD_COLOR_INDEX | None:
159159
highlight = self.highlight
160160
if highlight is None:
161161
return None
162+
# -- `w:val="none"` is a valid `ST_HighlightColor` value meaning "no
163+
# -- highlight". It has no `WD_COLOR_INDEX` member, so map it to `None`
164+
# -- (the same value used when no `w:highlight` element is present)
165+
# -- rather than raising on the unmapped value.
166+
if highlight.get(qn("w:val")) == "none":
167+
return None
162168
return highlight.val
163169

164170
@highlight_val.setter

tests/text/test_font.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def it_can_change_its_underline_type(
381381
("w:r/w:rPr", None),
382382
("w:r/w:rPr/w:highlight{w:val=default}", WD_COLOR.AUTO),
383383
("w:r/w:rPr/w:highlight{w:val=blue}", WD_COLOR.BLUE),
384+
("w:r/w:rPr/w:highlight{w:val=none}", None),
384385
],
385386
)
386387
def it_knows_its_highlight_color(self, r_cxml: str, expected_value: WD_COLOR | None):

0 commit comments

Comments
 (0)