Automation for importing images into Microsoft word using Python
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.

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
- python
- shell script
Prerequisites
To have python 3.86 installs in your computer
Install python method:
Navigate to https://www.python.org/downloads/release/python-386/ 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:
.
├── README.md
├── imgs
│ ├── # Where you place the target images
├── main.py
├── output
│ └── demo.docx
├── requirements.txt
└── run.sh
Run.sh
The entry point of the project.
#!/bin/bashif [[ "$OSTYPE" == "darwin"* ]];
then
# Mac OSX
pip install -r requirements.txt
python main.py
elif [[ "$OSTYPE" == "win32" ]];
then
pip install -r requirements.txt
py main.py
else
pip install -r requirements.txt
py main.py
fi
Requirements.txt
The file where you specify the dependency of your python program.
python-docx
Main.py
The main program
# For reading file name
import glob
print(glob.glob('.'))myList=[]
myListWithoutSlash=[]
for file_name in glob.iglob('./imgs/*.*', recursive=True):
print(file_name)
myList.append(file_name)
# using split()
# Get String after substring occurrence
res = file_name.split('/')
for item in res:
if "jpeg" in item:
myListWithoutSlash.append(item)
if "JPEG" in item:
myListWithoutSlash.append(item)
if "png" in item:
myListWithoutSlash.append(item)
if "PNG" in item:
myListWithoutSlash.append(item)
if "jpg" in item:
myListWithoutSlash.append(item)
if "JPG" in item:
myListWithoutSlash.append(item)# for python-docx package
# create words documents
from docx import Document
from docx.shared import Cmdocument = Document()table = document.add_table(rows = 1, cols = 4)
table.style = '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)):
print(i)
print(myListWithoutSlash[i])
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))document.save('./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 run.sh
./run.sh
Validation
Go to folder output
and open the demo.docx

Summary
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.