python-docx Assigning text to image

Viewed 13

I have this code:

    document = Document()
    document.add_heading("first comparison", 4)



    # Add text:
    paragraph = document.add_paragraph()
    run = paragraph.add_run()
    run.add_text("dog 1:")

    run_2 = paragraph.add_run()
    run_2.add_text("dog 2:")

    # Add images
    image_height = Cm(8)
    image_width = Cm(5)

    paragraph = document.add_paragraph()
    run = paragraph.add_run()
    run.add_picture("./dog1.jpeg", width=image_width,
                    height=image_height)
    run_2 = paragraph.add_run()
    run_2.add_picture("dog2.jpeg", width=image_width, height=image_height)

    document.save('test.docx')

enter image description here

I would like this to be the output: enter image description here

Some of my images has different width, so I don't want to manually add spaces to my text How can I solve it?

0 Answers
Related