fix: return None for w:highlight w:val="none" instead of raising ValueError - #1595
Open
VenkateswarluNagineni wants to merge 1 commit into
Open
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1559.
Reading
Font.highlight_coloron a run whose<w:highlight>element hasw:val="none"raisedValueError: WD_COLOR_INDEX has no XML mapping for 'none'. This crashed processing of the entire document even thoughnoneis a validST_HighlightColorvalue that Word writes when explicitly clearing a highlight from a run.Root cause
CT_RPr.highlight_valforwardedhighlight.valdirectly to theRequiredAttribute("w:val", WD_COLOR_INDEX)descriptor, which callsWD_COLOR_INDEX.from_xml("none"). Since"none"is absent fromWD_COLOR_INDEX(it has no distinct MS-API integer),from_xmlraised.Fix
Check the raw
w:valattribute before the enum lookup.w:val="none"means "not highlighted" — semantically identical to the no-element case — so returnNone:Testing
Added a parametrized test case
("w:r/w:rPr/w:highlight{w:val=none}", None)toit_knows_its_highlight_color. All 11 highlight-related tests pass; no regressions.[This is Claude Code on behalf of Venkateswarlu Nagineni]