add two images in same line in python-docx
Asked Answered
P

1

6

I am trying to add two images in docx file. Images should be one left side one right side. After using this below code the image position is working like left and right as I want but they are not on the same line I want. One is up and others are under that image.

I have tried the WD_ALIGN_PARAGRAPH.RIGHT but I am not getting the result I want.

## Image for Left Side
my_img = document.add_picture(i,width=Inches(0.8),height=Inches(0.8))
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT

## Image for Right Side
my_img2 = document.add_picture(i,width=Inches(0.8),height=Inches(0.8))
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

Please help me, I want both images on same line like two images together with little space between them.

enter image description here

Prehensible answered 5/8, 2019 at 15:43 Comment(0)
A
11

Use Run.add_picture() instead of Paragraph.add_picture(). This will allow multiple images in the same paragraph, which, if they both fit within the page margins, will result in side-by-side images:

paragraph = document.add_paragraph()
run = paragraph.add_run()
run.add_picture(...)
run_2 = paragraph.add_run()
run_2.add_picture(...)

As far as alignment is concerned, when using paragraphs, inserting tabs is probably most reliable. The other alternative is to add a table and place the images in side-by-side cells.

Aixenprovence answered 5/8, 2019 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.