Automation for importing images into Microsoft word using Python

Photo by Davide Santillo on Unsplash

Real-life problem:

Import image into documents with tables is a tedious process. How’s programming can help with this problem?

Today I would like to share my 2 cents on solving real-life problem: how to import images into documents in an automated manner.

Background: what is python?

Python is an interpreted, object-oriented, high-level programming language.

photo from

You can develop a python program in Windows, Linux, Ipad OS, IOS, and more platforms/ operating systems(OS). Its portability makes it great for seamlessly connect and ‘glue’ on each platform for large software programs. It spans on multiple platforms, middleware products, and application domains.

Technologies used

  1. python
  2. shell script


To have python 3.86 installs in your computer

Install python method:

Navigate to and download the respective installer according to your platform/OS.

Getting Started

You can get the source code from here

This project has the following structure:

├── imgs
│ ├── # Where you place the target images
├── output
│ └── demo.docx
├── requirements.txt

The entry point of the project.

#!/bin/bashif [[ "$OSTYPE" == "darwin"* ]]; 
# Mac OSX
pip install -r requirements.txt
elif [[ "$OSTYPE" == "win32" ]];
pip install -r requirements.txt
pip install -r requirements.txt


The file where you specify the dependency of your python program.


The main program

# For reading file name
import glob
for file_name in glob.iglob('./imgs/*.*', recursive=True):
# using split()
# Get String after substring occurrence
res = file_name.split('/')
for item in res:
if "jpeg" in item:
if "JPEG" in item:
if "png" in item:
if "PNG" in item:
if "jpg" in item:
if "JPG" in item:
# for python-docx package
# create words documents
from docx import Document
from docx.shared import Cm
document = Document()table = document.add_table(rows = 1, cols = 4) = 'Table Grid'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'ProductName'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'ImageName'
hdr_cells[3].text = 'Image'
for i in range(len(myList)):
row_cells = table.add_row().cells
row_cells[2].text = myListWithoutSlash[i]
p = row_cells[3].add_paragraph()
r = p.add_run()
r.add_picture(myList[i],width=Cm(4.0), height=Cm(4))'./output/demo.docx')

Firstly, we list the directory in imgs and assume the user have put the targets images into the folder imgs and we construct a list to hold the images name.

Secondly, we make use of the python-docx python package and create a table with titles.

Finally, we input the images into the tables.

Start the program

Open your terminal or command prompt and navigate to this project directory.

chmod +x


Go to folder output and open the demo.docx

Table with images


By using python, we have created a program that can create tables with imported images. This program helps us to save precious time and able to focus on important tasks!

Please leave any comments for any suggestions or improvements! If you spot any errors or have questions for me, feel free to contact me.

Hope all readers are well, staying safe and healthy.

See you next time!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Redshift: Convert TEXT to Timestamp

Recommended System Architecture Principles

System Architecture

Early on clinicians taking care patients with COVID-19

Just What We Need …. More News

Vulnhub: JIS-CTF-VulnUpload Write-Up

How I built a $150,000 mobile app for $200

A pack of text-related updates: paste without formatting, list styles, and more

First Day of Programming Class

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniel Chu

Daniel Chu

More from Medium

ElasticSearch Python

Recognize a Photo of a Form, Extract Key Fields and Business Information in Python

Deploy model with FastAPI and Heroku

Secure a FastAPI Server with Asgardeo