• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

    Streaming Audio Files Securely with PHP

    Streaming Audio Files Securely with PHP

    Scraping Web Data with Python Helium

    Scraping Web Data with Python Helium

    Building a Secure 2FA Authenticator with Python

    Building a Secure 2FA Authenticator with Python

    Building a Cache Warmer with Python

    Building a Cache Warmer with Python

    How to Create a Python GUI to Launch Webhooks

    How to Create a Python GUI to Launch Webhooks

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

    38 essential points to harden WordPress

    38 Essential Points to Harden WordPress

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

    Streaming Audio Files Securely with PHP

    Streaming Audio Files Securely with PHP

    Scraping Web Data with Python Helium

    Scraping Web Data with Python Helium

    Building a Secure 2FA Authenticator with Python

    Building a Secure 2FA Authenticator with Python

    Building a Cache Warmer with Python

    Building a Cache Warmer with Python

    How to Create a Python GUI to Launch Webhooks

    How to Create a Python GUI to Launch Webhooks

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

    38 essential points to harden WordPress

    38 Essential Points to Harden WordPress

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
Home Guides Programming Python

Mastering python-docx: A Guide to Creating Word Documents with Python

Jonathan Moore by Jonathan Moore
2 years ago
Reading Time: 8 mins read
A A
Mastering python-docx A Guide to Creating Word Documents with Python
FacebookTwitter

Python provides robust tools for working with Microsoft Word documents. One of the most powerful libraries available for this purpose is python-docx. This library allows you to create, modify, and extract information from Word documents (.docx) using Python. In this article, we’ll explore the capabilities of python-docx and demonstrate how to create a .docx document from scratch.

Getting Started

Before we dive into the functionality, you need to install the python-docx library. You can do this using pip:

pip install python-docx

With the library installed, you can now start creating and manipulating Word documents.

Creating a Word Document

The first step in working with python-docx is creating a new Word document. This is done by initializing a Document object.

from docx import Document

# Create a new Document
doc = Document()

Adding a Title

You can add a title to your document by using the add_heading method. This method allows you to specify the level of the heading (1 through 9).

# Add a title
doc.add_heading('Document Title', level=1)

Adding Paragraphs

Adding paragraphs is straightforward with the add_paragraph method. This method returns a Paragraph object that you can further manipulate if needed.

# Add a paragraph
doc.add_paragraph('This is the first paragraph of the document.')

Formatting Text

python-docx allows you to apply basic text formatting, such as bold, italic, and underline. This is done using Run objects, which represent contiguous runs of text with the same formatting.

# Add a paragraph with formatted text
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is bold and italic.')
run.bold = True
run.italic = True

Changing Font Size and Color

To change the font size and color of text, you use the Font object associated with a Run.

from docx.shared import Pt
from docx.shared import RGBColor

# Add a paragraph with custom font size and color
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is 24 pt and blue.')
run.font.size = Pt(24)
run.font.color.rgb = RGBColor(0, 0, 255)

Creating a Blockquote

To create a blockquote, you can adjust the indentation and apply a different font style to a paragraph.

# Add a blockquote
blockquote = doc.add_paragraph('This is a blockquote paragraph. It is typically indented and styled differently.')
blockquote.paragraph_format.left_indent = Inches(0.5)
blockquote.paragraph_format.space_before = Pt(12)
blockquote.paragraph_format.space_after = Pt(12)
blockquote.runs[0].italic = True

Text Alignment

You can align text within a paragraph using the alignment attribute of the Paragraph object. Available alignment options include left, center, right, and justified.

from docx.enum.text import WD_ALIGN_PARAGRAPH

# Add a paragraph with centered text
paragraph = doc.add_paragraph('This paragraph is centered.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Add a paragraph with right-aligned text
paragraph = doc.add_paragraph('This paragraph is right-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

# Add a paragraph with left-aligned text
paragraph = doc.add_paragraph('This paragraph is left-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT

# Add a paragraph with justified text
paragraph = doc.add_paragraph('This paragraph is justified.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY

Adding Lists

You can add both ordered and unordered lists using the add_paragraph method with appropriate styles.

# Add an unordered list
doc.add_paragraph('First item in unordered list', style='List Bullet')
doc.add_paragraph('Second item in unordered list', style='List Bullet')

# Add an ordered list
doc.add_paragraph('First item in ordered list', style='List Number')
doc.add_paragraph('Second item in ordered list', style='List Number')

Inserting Images

Inserting images into your document is simple with the add_picture method. You can also specify the width and height of the image.

from docx.shared import Inches

# Add an image
doc.add_picture('path/to/image.jpg', width=Inches(2))

Creating Tables

python-docx makes it easy to create tables and populate them with data.

# Create a table
table = doc.add_table(rows=3, cols=3)

# Populate the table
for row in table.rows:
    for cell in row.cells:
        cell.text = 'Cell content'

Saving the Document

Once you have added all your content, you can save the document using the save method.

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

Full Example Script

Here’s a complete script that demonstrates all the functionalities mentioned above:

#!/usr/bin/env python3
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH

# Create a new Document
doc = Document()

# Add a title
doc.add_heading('Document Title', level=1)

# Add a paragraph
doc.add_paragraph('This is the first paragraph of the document.')

# Add a paragraph with formatted text
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is bold and italic.')
run.bold = True
run.italic = True

# Add an unordered list
doc.add_paragraph('First item in unordered list', style='List Bullet')
doc.add_paragraph('Second item in unordered list', style='List Bullet')

# Add an ordered list
doc.add_paragraph('First item in ordered list', style='List Number')
doc.add_paragraph('Second item in ordered list', style='List Number')

# Add an image
doc.add_picture('path/to/image.jpg', width=Inches(2))

# Create a table
table = doc.add_table(rows=3, cols=3)

# Populate the table
for row in table.rows:
    for cell in row.cells:
        cell.text = 'Cell content'

# Add a paragraph with centered text
paragraph = doc.add_paragraph('This paragraph is centered.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Add a paragraph with right-aligned text
paragraph = doc.add_paragraph('This paragraph is right-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

# Add a paragraph with left-aligned text
paragraph = doc.add_paragraph('This paragraph is left-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT

# Add a paragraph with justified text
paragraph = doc.add_paragraph('This paragraph is justified.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY

# Add a paragraph with custom font size and color
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is 24 pt and blue.')
run.font.size = Pt(24)
run.font.color.rgb = RGBColor(0, 0, 255)

# Add a blockquote
blockquote = doc.add_paragraph('This is a blockquote paragraph. It is typically indented and styled differently.')
blockquote.paragraph_format.left_indent = Inches(0.5)
blockquote.paragraph_format.space_before = Pt(12)
blockquote.paragraph_format.space_after = Pt(12)
blockquote.runs[0].italic = True

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

Advanced Features

Adding Sections

Word documents can have multiple sections, each with its own page layout settings. You can add sections using the add_section method.

from docx.enum.section import WD_SECTION

# Add a new section
section = doc.add_section(WD_SECTION.NEW_PAGE)

Page Breaks

To insert a page break, you can use the add_page_break method.

# Add a page break
doc.add_page_break()

Setting Margins

You can set the margins of a section using the Section object.

from docx.shared import Inches

# Set margins for the section
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1)
section.right_margin = Inches(1)

Adding Headers and Footers

Headers and footers can be added to sections. You can insert text, images, and even tables in headers and footers.

# Add a header
header = section.header
header_paragraph = header.paragraphs[0]
header_paragraph.text = "Header text"

# Add a footer
footer = section.footer
footer_paragraph = footer.paragraphs[0]
footer_paragraph.text = "Footer text"

Adding Page Numbers to the Footer

To add page numbers to the footer, you need to insert a Field object into the footer.

from docx.oxml import OxmlElement
from docx.oxml.ns import qn

# Add page number to the footer
footer_paragraph = footer.paragraphs[0]
run = footer_paragraph.add_run()
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve')
instrText.text = 'PAGE'
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'end')
run._r.append(fldChar1)
run._r.append(instrText)
run._r.append(fldChar2)

Complete Advanced Example

Here’s a complete script that includes advanced features like adding sections, setting margins, and adding headers, footers, and page numbers.

#!/usr/bin/env python3
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.section import WD_SECTION
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH

# Create a new Document
doc = Document()

# Add a title
doc.add_heading('Document Title', level=1)

# Add a paragraph
doc.add_paragraph('This is the first paragraph of the document.')

# Add a paragraph with formatted text
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is bold and italic.')
run.bold = True
run.italic = True

# Add an unordered list
doc.add_paragraph('First item in unordered list', style='List Bullet')
doc.add_paragraph('Second item in unordered list', style='List Bullet')

# Add an ordered list
doc.add_paragraph('First item in ordered list', style='List Number')
doc.add_paragraph('Second item in ordered list', style='List Number')

# Add an image
doc.add_picture('Mastering python-docx A Guide to Creating Word Documents with Python.jpg', width=Inches(2))

# Create a table
table = doc.add_table(rows=3, cols=3)

# Populate the table
for row in table.rows:
    for cell in row.cells:
        cell.text = 'Cell content'

# Add a paragraph with centered text
paragraph = doc.add_paragraph('This paragraph is centered.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Add a paragraph with right-aligned text
paragraph = doc.add_paragraph('This paragraph is right-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

# Add a paragraph with left-aligned text
paragraph = doc.add_paragraph('This paragraph is left-aligned.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.LEFT

# Add a paragraph with justified text
paragraph = doc.add_paragraph('This paragraph is justified.')
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY

# Add a paragraph with custom font size and color
paragraph = doc.add_paragraph()
run = paragraph.add_run('This text is 24 pt and blue.')
run.font.size = Pt(24)
run.font.color.rgb = RGBColor(0, 0, 255)

# Add a blockquote
blockquote = doc.add_paragraph('This is a blockquote paragraph. It is typically indented and styled differently.')
blockquote.paragraph_format.left_indent = Inches(0.5)
blockquote.paragraph_format.space_before = Pt(12)
blockquote.paragraph_format.space_after = Pt(12)
blockquote.runs[0].italic = True

# Add a new section
section = doc.add_section(WD_SECTION.NEW_PAGE)
paragraph = doc.add_paragraph('This is a new page.')

# Set margins for the section
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1)
section.right_margin = Inches(1)

# Add a header
header = section.header
header_paragraph = header.paragraphs[0]
header_paragraph.text = "Header text"

# Add a footer
footer = section.footer
footer_paragraph = footer.paragraphs[0]
footer_paragraph.text = "Footer text\n"
footer_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Add page number to the footer
run = footer_paragraph.add_run()
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve')
instrText.text = 'PAGE'
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'end')
run._r.append(fldChar1)
run._r.append(instrText)
run._r.append(fldChar2)

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

Conclusion

python-docx is a powerful library for creating and manipulating Word documents programmatically. This guide covered the basics of document creation, including adding text, formatting, lists, images, and tables. We also touched on advanced features like sections, page breaks, margins, headers, footers, and adding page numbers. With these tools, you can automate document generation, create templates, and enhance productivity in your projects. Feel free to explore the official documentation for python-docx to discover more features and customize your document processing tasks even further.

Tags: docxMicrosoft WordPython
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

Senior Software Engineer and Cybersecurity Specialist with over 3 decades of experience in developing web, desktop, and server applications for Linux and Windows-based operating systems. Worked on numerous projects, including automation, artificial intelligence, data analysis, application programming interfaces, intrusion detection systems, streaming audio servers, WordPress plugins, and much more.

Related Articles

Monitoring Web Page Changes with Python

Monitoring Web Page Changes with Python

There are times when I need to know that a web page has changed without actively watching it. That might...

Building a Network Tracker Auditor for Privacy with Python

Building a Network Tracker Auditor for Privacy with Python

In my last post, I dug into AdGuard, a robust ad blocker that tackles trackers and ads head-on. But how...

Scraping Web Data with Python Helium

Scraping Web Data with Python Helium

If you've ever needed to extract information from a website programmatically, you've likely heard of various tools and libraries. One...

Next Post
How to Create a Python GUI to Launch Webhooks

How to Create a Python GUI to Launch Webhooks

Recommended Services

Latest Articles

Building a Simple WordPress Post List Tool with PHP

Building a Simple WordPress Post List Tool with PHP

I needed a quick way to view all my WordPress posts without logging into the admin dashboard. Sometimes you just...

Read moreDetails

Why Stable Websites Outperform Flashy Redesigns

Why Stable Websites Outperform Flashy Redesigns

Most websites do not fail in dramatic fashion. There is no explosion, no warning siren, no obvious moment where everything...

Read moreDetails

Monitoring Web Page Changes with Python

Monitoring Web Page Changes with Python

There are times when I need to know that a web page has changed without actively watching it. That might...

Read moreDetails

My SSH Setup: How I Manage Multiple Servers

My SSH Setup: How I Manage Multiple Servers

If you work with more than one server, the need to manage multiple servers with SSH becomes obvious pretty quickly....

Read moreDetails
  • Privacy Policy
  • Terms of Service

© 2025 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Tutorials
    • Privacy
    • Security
  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
    • General Contact
    • Website Administration & Cybersecurity