'Python-docx: convert type <class 'docx.oxml.text.run.CT_R'> to <class 'docx.text.run.Run'> to apply a highlight

I have a function using python-docx which outputs a CT_R object. I'd like to be able to convert it to a Run object such that I can then apply a styling change, something like:

font.highlight_color = WD_COLOR_INDEX.YELLOW

As it stands, trying to apply the styling change to the CT_R object is giving an error. How can I convert the object of type <class 'docx.oxml.text.run.CT_R'> to an object of type <class 'docx.text.run.Run'>? I tried to write

Run(r, paragraph)

with 'r' being the CT_R variable, but I am getting an error that "NameError: name 'Run' is not defined".

I think a run is the parent class of CT_R but I cannot figure out the conversion. Thanks for the help in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source