Skip to content

Instantly share code, notes, and snippets.

@farooqarahim
Last active April 21, 2023 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farooqarahim/9d406d9edcc1dd76b2595cdec283875d to your computer and use it in GitHub Desktop.
Save farooqarahim/9d406d9edcc1dd76b2595cdec283875d to your computer and use it in GitHub Desktop.
Create a word document [A4] using photos from a folder.
# !pip install python-docx Pillow
import os
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from PIL import Image
def build_document(images_folder_path, output_path, columns, rows, add_text):
# List all files in the folder
file_list = os.listdir(folder_path)
# Filter only image files
image_files = [file for file in file_list if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif'))]
# Create a new Word document
doc = Document()
# Set the margins for the document
margin = 0.5 # inches
for section in doc.sections:
section.top_margin = Inches(margin)
section.bottom_margin = Inches(margin)
section.left_margin = Inches(margin)
section.right_margin = Inches(margin)
# Calculate cell dimensions based on A4 page size, margins, and desired columns
page_width = 8.27 # inches
page_height = 11.69 # inches
cell_width = (page_width - 2 * margin) / columns
cell_height = (page_height - 2 * margin) / rows
images_per_page = columns * rows
# Loop through the image files and insert them into the Word document
table = None
row = None
for index, image_file in enumerate(image_files):
if index % images_per_page == 0:
# Create a new table with the desired number of rows and columns
table = doc.add_table(rows=rows, cols=columns)
table.autofit = False
# Set the dimensions of the table cells
for r in range(len(table.rows)):
for c in range(len(table.rows[r].cells)):
cell = table.rows[r].cells[c]
cell.width = Inches(cell_width)
cell.height = Inches(cell_height)
row = 0
# Open the image file and add it to the corresponding cell
with Image.open(os.path.join(folder_path, image_file)) as img:
cell = table.cell(row, index % columns)
# Add the image number text box above the image if add_text is True
if add_text:
paragraph = cell.add_paragraph(f"Image {index + 1}")
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
paragraph = cell.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = paragraph.add_run()
run.add_picture(img.filename, width=Inches(cell_width))
# Move to the next row after adding the desired number of images per row
if index % columns == columns - 1:
row += 1
# Add a new page after adding the desired number of images per page, except for the last iteration
if index % images_per_page == images_per_page - 1 and index != len(image_files) - 1:
doc.add_page_break()
# Save the Word document
doc.save(output_path)
# Set the folder path with images and the desired output file path
images_folder_path = 'input/'
output_path = 'output/output.docx'
# Number of columns per page
columns = 3
# Number of rows per page
rows = 3
# Option to add Additional Text Box
add_text = False
build_document(images_folder_path, output_path, columns, rows, add_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment