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

Creating Microsoft Word documents programmatically in Python is essential for automating report generation. The python-docx library provides a simple interface to create paragraphs, add text formatting, and insert images into Word documents.

Installing python-docx

First, install the required library using pip ?

pip install python-docx

Creating Paragraphs and Adding Text

Start by creating a new document and adding paragraphs with text ?

import docx

# Create a new document
word_doc = docx.Document()

# Add a paragraph
paragraph = word_doc.add_paragraph('1. Hello World, Some Sample Text Here...')
run = paragraph.add_run()

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

# Add more text to the same paragraph
paragraph.add_run('2. I have just written my 2nd line and I can write more..')

# Save the document
word_doc.save('My_Document.docx')
print("Document created successfully!")
Document created successfully!

Reading Document Content

You can verify the document content programmatically ?

import docx

# Open the document
doc = docx.Document('My_Document.docx')

print(f"Document has {len(doc.paragraphs)} paragraphs")

for paragraph_number, paragraph in enumerate(doc.paragraphs):
    if paragraph.text:
        print(f"Paragraph {paragraph_number + 1}: {paragraph.text}")
Document has 1 paragraphs
Paragraph 1: 1. Hello World, Some Sample Text Here...
2. I have just written my 2nd line and I can write more..

Adding Images to Documents

Download an image and add it to your Word document with proper sizing ?

import docx
import requests
from docx.shared import Cm

# Create a new document
doc = docx.Document()

# Add some text first
doc.add_paragraph('This document contains an image below:')

# Download an image (requires internet connection)
response = requests.get("https://raw.githubusercontent.com/sasankac/TestDataSet/master/Tree.jpg")
with open("Tree.jpg", "wb") as image_file:
    image_file.write(response.content)

# Add the image to document
image_to_add = doc.add_picture("Tree.jpg")

print(f"Original image dimensions:")
print(f"Width: {image_to_add.width}, Height: {image_to_add.height}")

# Resize the image
image_to_add.width = Cm(10)
image_to_add.height = Cm(8)

print(f"Resized image dimensions:")
print(f"Width: {image_to_add.width}, Height: {image_to_add.height}")

# Save the document
doc.save('document_with_image.docx')

Complete Example

Here's a complete example that creates a formatted document with text and images ?

import docx
from docx.shared import Cm

# Create a new document
doc = docx.Document()

# Add a title paragraph
title = doc.add_paragraph()
title_run = title.add_run('Report: Sample Document')
title_run.bold = True
title_run.font.size = docx.shared.Pt(16)

# Add some content paragraphs
doc.add_paragraph('This is the first paragraph of our report.')

# Add a paragraph with formatting
para2 = doc.add_paragraph('This paragraph has ')
bold_run = para2.add_run('bold text')
bold_run.bold = True
para2.add_run(' and continues normally.')

# Add a page break
doc.add_page_break()

# Add another paragraph on the new page
doc.add_paragraph('This text appears on the second page.')

# Save the document
doc.save('formatted_report.docx')

print("Formatted document created successfully!")
print(f"Document contains {len(doc.paragraphs)} paragraphs")
Formatted document created successfully!
Document contains 4 paragraphs

Key Features

Feature Method Description
Create Document docx.Document() Creates new Word document
Add Paragraph add_paragraph() Adds text paragraph
Add Image add_picture() Inserts image with sizing
Text Formatting add_run() Applies bold, italic, font size

Conclusion

The python-docx library simplifies Word document creation in Python. Use add_paragraph() for text content and add_picture() for images. Remember to resize images using the Cm unit for proper formatting.

Updated on: 2026-03-25T12:02:39+05:30

565 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements