I have a folder called InputFiles where I have multiple .doc type files with an image in each file. I want python to go to each document search, find and replace an image and then save the document in the OutputFiles. The code currently opens and saves the document if is an .rft but will not save the image, just the document. How do I get it to work with .doc and do what I need it to do.
The image file is saved in a folder called Pictures and the picture is called HR_simo.png
import os
from docx import Document
document = Document()
sourcepath = os.listdir ('InputFiles/')
for file in sourcepath:
inputfile = 'InputFiles/' +file
with open (inputfile, 'r') as inputfile:
filedata = inputfile.read()
document.add_picture('Pictures/HR_Simo.png', width=Inches(1.25))
destinationpath ='OutputFiles/' +file
with open(destinationpath, 'w') as file:
file.write(filedata)