Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to export images to word file?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading