Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

python - How to extract text and images from a docx file and write it to new docx file with few changes

I am trying to replacing the text using loop on doc.paragrapgh .I got success in replacing the text ,but the images got removed .I want to preserve the images and only want to replace the text . Thanks in advance i am using below code for the same

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')
question from:https://stackoverflow.com/questions/65843126/how-to-extract-text-and-images-from-a-docx-file-and-write-it-to-new-docx-file-wi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...