Reputation: 25
I am working on a script which saves images to word file. I have been able to create a function to export images.
def exp_images(): #export images to word file
document = Document()
paragraph = document.add_paragraph()
script = paragraph.add_run()
script.add_text('This is a test sentence')
script.add_picture('image1.png', width=Inches(6.5))
document.save('demo.docx')
if __name__ == "__main__":
exp_images()
The only issue I am facing is how to call that function repeatedly, and the function must save image to new line, and must not overwrite the existing image. So, I just need help with this. Thanks
Upvotes: 1
Views: 781
Reputation: 38552
You can pass all the images to your function, iterate all of them, append them to the document and finally save the document.
def exp_images(images): # export images to word file
document = Document()
paragraph = document.add_paragraph()
script = paragraph.add_run()
script.add_text('This is a test sentence')
# loop through a list of images and add each one to the document
for image in images:
# add a line break between the text and the image
script.add_break(WD_BREAK.LINE)
# add the image to the document
script.add_picture(image, width=Inches(6.5))
document.save('demo.docx')
if __name__ == "__main__":
# call the exp_images function multiple times, loop through a list of images and add each one to the document
images = ['image1.png', 'image2.png', 'image3.png']:
exp_images(images)
Upvotes: 1