1

I can insert multiple images into a word document using the code that follows but they appear beneath each other. Is there a way of inserting them so that they are next to each other instead?

from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

document.add_picture(image_filepath, width=Inches(1.25))
document.add_picture(image_filepath, width=Inches(1.25))

document.save(doc_filepath)
Samuel Dion-Girardeau
  • 2,790
  • 1
  • 29
  • 37
Neil
  • 706
  • 8
  • 23

1 Answers1

4

One way to do it is to create transparent table and add images to cells:

from docx import Document

document = Document()
tables = document.tables
table = document.add_table(rows=1, cols=2)
row_cells = table.add_row().cells

for i, image in enumerate(['image1.jpg', 'image2.jpg']):
    paragraph = row_cells[i].paragraphs[0]
    run = paragraph.add_run()
    run.add_picture(image)
document.save('doc.docx')

That's what you'll get for instance:

enter image description here

Alderven
  • 7,569
  • 5
  • 26
  • 38