如何从 docx 文件中提取文本和图像,并将其写入新的 docx 文件中,只需进行少量更改

问题描述

我正在尝试使用 doc.paragrapgh 上的循环替换文本。我成功替换了文本,但图像被删除了。我想保留图像,只想替换文本。预先感谢我使用下面的代码

from docx import Document
from docx.table import _Cell

doc = Document("abc.docx")
for p in doc.paragraphs:
    if 'CORREDOR' in p.text:
        text = p.text.replace(p.text,"VIGENCIA").strip()
        style = p.style
        p.text = text
        p.style = style
    elif 'DECLaraCIÓN DE NO SINIESTRALIDAD' in p.text:
        text=  p.text.replace(p.text,"DECLaraCIÓN DE  SINIESTRALIDAD")
        style = p.style
        p.text = text
        p.style = style

doc.save('result4.docx')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)