问题
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