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 https://www.python.org/

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

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 Cm
document = 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

Table with images

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.

Hope all readers are well, staying safe and healthy.

See you next time!

--

--

--

https://www.youracclaim.com/users/jia-hao-chu/

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

https://www.youracclaim.com/users/jia-hao-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