I’m working on an assignment where I need to take a template word doc (“Template.docx”), and replace certain portions of the text and some of the images as well. I’m done with the text side of things, but I can’t figure out how replace images in a word doc.
My best attempt so far has been using the win32com library to loop through all the group objects and finding the ones that are pictures. I can’t figure out what methods are available to actually manipulate those picture objects however.
It’s important that I retain the position and size of each image as well, so the format of the template stays the same.
I’m open to any solutions that someone might have.
Thanks
doc = word.Documents.Open(current_dir + '\Template.docx')
# This replaces the text located within textboxes (and should replace pictures as with other pictures as well)
for sh in doc.Shapes:
if sh.Type == 6: # 6 is a group
for grp in sh.GroupItems:
if grp.Type == 13:
grp.**ReplacePictureMethodHere**
for i in ProjectParameters:
while grp.Type == 1:
t = grp.TextFrame.TextRange
if "${"+i+"}" in t.Text:
t.Find.Execute(
FindText="${"+i+"}", ReplaceWith=ProjectParameters[i], Forward=True)
else:
break