0

I have google this error and find no-one else encounter this ValueError before. You could see from the traceback log below that the error is triggered by my code line bg_color = run.font.highlight_color I guess python-docx library encounter some weird case that it cannot handle? Maybe it's some WD_COLOR_INDEX value that contain in the docx that python-docx do not recognize.

If python-docx meet and place where there's no font.hightlight_color, it should just return a None to me instead trying to get a translation value from some xml mapping.

How could I fixed this?

Traceback (most recent call last):
  File "XXX.py", line 377, in <module>
    main()
  ...
    bg_color = run.font.highlight_color
  File "/home/xxx/.local/lib/python3.8/site-packages/docx/text/font.py", line 139, in highlight_color
    return rPr.highlight_val
  File "/home/xxx/.local/lib/python3.8/site-packages/docx/oxml/text/font.py", line 183, in highlight_val
    return highlight.val
  File "/home/xxx/.local/lib/python3.8/site-packages/docx/oxml/xmlchemy.py", line 254, in get_attr_value
    return self._simple_type.from_xml(attr_str_value)
  File "/home/xxx/.local/lib/python3.8/site-packages/docx/enum/base.py", line 64, in from_xml
    raise ValueError(f"{cls.__name__} has no XML mapping for '{xml_value}'")
ValueError: WD_COLOR_INDEX has no XML mapping for 'none'

1 Answer 1

0

I try to find the cause. So I reduce the target docx to only the key content that will trigger the error. Then I rename the docx to xxx.zip and unzip it. I look at the document.xml file and see some code like this:<w:highlight w:val="none"/><w:shd w:val="clear" w:fill="F7FAFC"/>.

At that time I think I have find the cause of the ValueError problem. python-docx library do not expect word file to have a (font) hightlight attribute which have a value of "none".

As it's a library bug and my purpose is to detect hightlighted runs so I just wrap my code around a try-except block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.