I want to set a style to a corrected word in MS Word text. Since it's not possible to change text style inside a run, I want to insert a new run with new style into the existing paragraph...
for p in document.paragraphs: for run in p.runs: if 'text' in run.text: new_run= Run()new_run.text='some new text' # insert this run into paragraph# smth like:p.insert(new_run)
How to do it?
p.add_run()
adds run to the end of paragraph, doesn't it?
Update
The best would be to be able to clone run (and insert it after a certain run). This way we reproduce the original run's style attributes in the new/cloned one.
Update 2
I could manage that insertion code:
if 'text' in run.text:new_run_element = CT_R() #._new() run._element.addnext(new_run_element)new_run = Run(new_run_element, run._parent)...
But after that:
- the paragraph runs number is left the same:
len(p.runs)
- as I save that doc in file, the MS Word fails to open it: