From fe990fb7389faff8586b00db077a422a2a62f449 Mon Sep 17 00:00:00 2001 From: VenkateswarluNagineni Date: Thu, 13 Aug 2026 13:50:52 -0500 Subject: [PATCH] fix: return None for w:highlight w:val="none" instead of raising ValueError Word writes `` when highlight is explicitly cleared from a run. This value is valid per ST_HighlightColor but absent from WD_COLOR_INDEX, so CT_RPr.highlight_val raised ValueError on any access to Font.highlight_color for such runs. Treat w:val="none" as "no highlight" (same semantics as the no-element case) by checking the raw attribute before the enum lookup. Fixes #1559 Co-Authored-By: Claude --- src/docx/oxml/text/font.py | 6 +++++- tests/text/test_font.py | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/docx/oxml/text/font.py b/src/docx/oxml/text/font.py index 32eb567ba..df87b40f0 100644 --- a/src/docx/oxml/text/font.py +++ b/src/docx/oxml/text/font.py @@ -8,7 +8,7 @@ from docx.enum.dml import MSO_THEME_COLOR from docx.enum.text import WD_COLOR_INDEX, WD_UNDERLINE -from docx.oxml.ns import nsdecls +from docx.oxml.ns import nsdecls, qn from docx.oxml.parser import parse_xml from docx.oxml.simpletypes import ( ST_HexColor, @@ -159,6 +159,10 @@ def highlight_val(self) -> WD_COLOR_INDEX | None: highlight = self.highlight if highlight is None: return None + # w:val="none" is a valid ST_HighlightColor value meaning "no highlight"; + # it's not in WD_COLOR_INDEX so we return None to match the no-element case. + if highlight.get(qn("w:val")) == "none": + return None return highlight.val @highlight_val.setter diff --git a/tests/text/test_font.py b/tests/text/test_font.py index 471c5451b..4416e9bea 100644 --- a/tests/text/test_font.py +++ b/tests/text/test_font.py @@ -381,6 +381,8 @@ def it_can_change_its_underline_type( ("w:r/w:rPr", None), ("w:r/w:rPr/w:highlight{w:val=default}", WD_COLOR.AUTO), ("w:r/w:rPr/w:highlight{w:val=blue}", WD_COLOR.BLUE), + # w:val="none" is a valid ST_HighlightColor meaning "no highlight" + ("w:r/w:rPr/w:highlight{w:val=none}", None), ], ) def it_knows_its_highlight_color(self, r_cxml: str, expected_value: WD_COLOR | None):