Skip to content

fix: return None for w:highlight w:val="none" instead of raising ValueError - #1595

Open
VenkateswarluNagineni wants to merge 1 commit into
python-openxml:masterfrom
VenkateswarluNagineni:fix/highlight-none-val-crash
Open

fix: return None for w:highlight w:val="none" instead of raising ValueError#1595
VenkateswarluNagineni wants to merge 1 commit into
python-openxml:masterfrom
VenkateswarluNagineni:fix/highlight-none-val-crash

Conversation

@VenkateswarluNagineni

Copy link
Copy Markdown

Summary

Fixes #1559.

Reading Font.highlight_color on a run whose <w:highlight> element has w:val="none" raised ValueError: WD_COLOR_INDEX has no XML mapping for 'none'. This crashed processing of the entire document even though none is a valid ST_HighlightColor value that Word writes when explicitly clearing a highlight from a run.

Root cause

CT_RPr.highlight_val forwarded highlight.val directly to the RequiredAttribute("w:val", WD_COLOR_INDEX) descriptor, which calls WD_COLOR_INDEX.from_xml("none"). Since "none" is absent from WD_COLOR_INDEX (it has no distinct MS-API integer), from_xml raised.

Fix

Check the raw w:val attribute before the enum lookup. w:val="none" means "not highlighted" — semantically identical to the no-element case — so 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

Testing

Added a parametrized test case ("w:r/w:rPr/w:highlight{w:val=none}", None) to it_knows_its_highlight_color. All 11 highlight-related tests pass; no regressions.


[This is Claude Code on behalf of Venkateswarlu Nagineni]

…eError

Word writes `<w:highlight w:val="none"/>` 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 python-openxml#1559

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reading Font.highlight_color raises ValueError for valid w:highlight w:val="none"

1 participant