How to create Microsoft Word paragraphs and insert Images in Python?


Introduction...

Being a Data Engineering specialist, I often receive test results from testers in Microsoft word. Sigh! they put so much information into word document right from capturing screen shots and very big paragraphs.

Other day, I was asked by testing team to help them with a program to insert the tool generated Text and images (taken by automatic screen shots. Not covered in this article).

MS Word document unlike others doesn't have the concept of a page, as it works in paragraphs unfortunately.So we need to use breaks and sections to properly divide a document.

How to do it..

1. Go ahead and install python-docx.

import docx

# create a new couments
WordDocx = docx.Document()

# My paragraph.
Paragraph = WordDocx.add_paragraph('1. Hello World, Some Sample Text Here...')
run = Paragraph.add_run()

# paragraph with a line break
run.add_break(docx.text.run.WD_BREAK.LINE)

# Add more
Paragraph.add_run('2. I have just written my 2nd line and I can write more..')

# Finally savind the document.
WordDocx.save('My_Amazing_WordDoc.docx')

2. Now Lets check the content if everything is OK or not. Well you are programmer, so we will do it programatically.

doc = docx.Document('My_Amazing_WordDoc.docx')
print(f"output \n *** Document has {len(doc.paragraphs)} - paragraphs")
for paragraph_number, paragraph in enumerate(doc.paragraphs):
if paragraph.text:
print(f"\n {paragraph.text}")

Output

*** Document has 1 - paragraphs

1. Hello World, Some Sample Text Here...
2. I have just written my 2nd line and I can write more..

3. Now we will add a image to our document. So, first we need to look for an image. I have downloaded an image from unsplash.com which doen't have any copyright issues. Please make sure what ever you download from internet do with utmost care.

Unsplash had copyright free images we can use for any purpose, appreciate their work.

Ok I have downloaded an image and named it as Tree.img which will be added to our document.

import requests
from docx.shared import Cm

# Download the image from Github
response = requests.get("https://raw.githubusercontent.com/sasankac/TestDataSet/master/Tree.jpg")
image = open("Tree.jpg", "wb")
image.write(response.content)
image.close()

# add the image
image_to_add = doc.add_picture("Tree.jpg")
print(f"output \n *** MY Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

Output

*** MY Image has width = 43891200 and Height as - 65836800

4. We need to scale the image properly as my Image is too big. we can use width and height parameter.

image_to_add.width = Cm(10)
image_to_add.height = Cm(10)
print(f" *** My New dimensions Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

# finally save the document
doc.save('report.docx')


*** My New dimensions Image has width = 3600000 and Height as - 3600000

5. Open the document and you would see the image and the text added.

6. Putting everything together.

Example

import requests
from docx.shared import Cm

# Download the image from Github
response = requests.get("https://raw.githubusercontent.com/sasankac/TestDataSet/master/Tree.jpg")
image = open("Tree.jpg", "wb")
image.write(response.content)
image.close()

# add the image
image_to_add = doc.add_picture("Tree.jpg")
print(f"output \n *** MY Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

image_to_add.width = Cm(10)
image_to_add.height = Cm(10)
print(f" *** My New dimensions Image has width = {image_to_add.width} and Height as - {image_to_add.height}")

# finally save the document
doc.save('report.docx')

Output

*** MY Image has width = 43891200 and Height as - 65836800
*** My New dimensions Image has width = 3600000 and Height as - 3600000

Updated on: 10-Nov-2020

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements