copying a paragraph containing inline mathematical formulas using python-docx

北慕城南 提交于 2020-03-06 09:28:38

问题


I am reading a docx file using python-docx and copying it paragraph by paragraph into another docx file (I am editting each paragraph). Some paragraphs contain inline mathematical formulas/equations, however this code ignores the equations and copies the remained text of each paragraph.

t= Document("E:\python\projects\test.docx")
temp= Document()
t_pars= list(t.paragraphs)
for i in range(len(t_pars)):
   temp.add_paragraph(t_pars[i].text) 
temp.save('E:\python\projects\temp.docx')

I know the problem comes from the 4th line where I use .text which receives plain text.

I have also tried using encode('utf-8') for t_pars[i] (via t_pars[i].encode('utf-8')) but is says that Paragraph hasn't got attribute encode. I have tested to edit the paragraphs without copying them into another file also, again, unsuccessful to maintain equations.

How can I read, edit and write paragraphs containing mathematical equations correctly and completely?

来源:https://stackoverflow.com/questions/60225970/copying-a-paragraph-containing-inline-mathematical-formulas-using-python-docx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!