Last modified: Nov 08, 2025 By Alexander Williams
Python-docx Paragraph Formatting Guide
Python-docx is a powerful library. It helps create and modify Word documents. This guide focuses on paragraph formatting.
You will learn to control text appearance. This includes alignment, spacing, and fonts. These skills make professional documents.
Getting Started with Python-docx
First, ensure python-docx is installed. If not, check our Install Python-docx for Word Documents guide. Installation is simple with pip.
After installation, import the library. Create a document object. This is your starting point for all operations.
from docx import Document
# Create a new document
doc = Document()
# Add a paragraph
paragraph = doc.add_paragraph("This is a sample paragraph.")
# Save the document
doc.save("formatted_document.docx")
This basic code creates a document. It adds one paragraph. The document saves as formatted_document.docx.
Paragraph Alignment Options
Alignment controls text positioning. Python-docx offers four main alignments. These are left, right, center, and justify.
Use the alignment property from WD_ALIGN_PARAGRAPH. Set it on your paragraph object. This affects the entire paragraph.
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph = doc.add_paragraph("This paragraph is center aligned.")
# Set alignment to center
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
doc.save("aligned_document.docx")
The paragraph will appear centered in Word. Similarly, use LEFT, RIGHT, or JUSTIFY. Each serves different layout needs.
Contracting Paragraph Spacing
Spacing affects readability. You can control space before and after paragraphs. Also, line spacing within paragraphs.
Access spacing through paragraph formatting. Use paragraph_format property. Then set space_before or space_after.
from docx import Document
from docx.shared import Pt
doc = Document()
paragraph = doc.add_paragraph("This paragraph has custom spacing.")
# Set spacing before and after
formatting = paragraph.paragraph_format
formatting.space_before = Pt(12)
formatting.space_after = Pt(12)
# Set line spacing to double
formatting.line_spacing = 2.0
doc.save("spaced_document.docx")
This adds space before and after. It also doubles line spacing. Adjust values as needed for your document.
Font Formatting within Paragraphs
Font styling makes text stand out. Change font name, size, color, and style. Apply these to specific text runs.
First, add a paragraph. Then add runs with add_run(). Style each run individually. This allows mixed formatting.
from docx import Document
from docx.shared import Pt
from docx.shared import RGBColor
doc = Document()
paragraph = doc.add_paragraph()
# Add and format a text run
run = paragraph.add_run("This is bold, red text.")
run.bold = True
run.font.color.rgb = RGBColor(255, 0, 0)
run.font.size = Pt(14)
# Add another run with different formatting
run2 = paragraph.add_run(" This is normal text.")
run2.font.size = Pt(12)
doc.save("styled_document.docx")
The first run appears bold and red. The second run uses default styling. Mixing styles in one paragraph is powerful.
Indentation and Tabs
Indentation creates visual hierarchy. Python-docx controls left, right, and first line indents. Use paragraph_format for these settings.
Set indentation in inches or points. Positive values indent inward. Negative values create hanging indents.
from docx import Document
from docx.shared import Inches
doc = Document()
paragraph = doc.add_paragraph("This paragraph has custom indentation.")
# Set various indents
formatting = paragraph.paragraph_format
formatting.left_indent = Inches(0.5)
formatting.right_indent = Inches(0.25)
formatting.first_line_indent = Inches(0.25)
doc.save("indented_document.docx")
This indents the left side by 0.5 inches. The right side by 0.25 inches. The first line has an additional indent.
Working with Multiple Paragraphs
Real documents contain many paragraphs. Apply formatting consistently. Loop through paragraphs for bulk changes.
After creating multiple paragraphs, access them through doc.paragraphs. This list contains all paragraphs.
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
# Add multiple paragraphs
doc.add_paragraph("First paragraph.")
doc.add_paragraph("Second paragraph.")
doc.add_paragraph("Third paragraph.")
# Format all paragraphs
for paragraph in doc.paragraphs:
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
doc.save("multiple_paragraphs.docx")
All paragraphs become justified. This approach saves time. It ensures consistent formatting throughout.
Advanced Paragraph Styling
Beyond basics, python-docx offers advanced features. These include borders, shading, and keeping lines together.
For more document creation techniques, see our Python-docx Tutorial: Create Word Documents. It covers broader document structure.
Set keep_with_next to prevent page breaks. This keeps headings with their content. Use paragraph_format for this property.
from docx import Document
doc = Document()
heading = doc.add_paragraph("Important Heading")
content = doc.add_paragraph("This content should stay with the heading.")
# Keep heading and content together
heading.paragraph_format.keep_with_next = True
doc.save("advanced_document.docx")
The heading and paragraph stay together. This prevents awkward page breaks. It improves document flow.
Common Formatting Examples
Here is a complete example. It combines multiple formatting techniques. This creates a professional-looking paragraph.
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt, Inches, RGBColor
doc = Document()
# Create a well-formatted paragraph
paragraph = doc.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
# Format the paragraph spacing
formatting = paragraph.paragraph_format
formatting.space_before = Pt(6)
formatting.space_after = Pt(6)
formatting.line_spacing = 1.5
formatting.left_indent = Inches(0.25)
# Add styled text
title_run = paragraph.add_run("Document Title: ")
title_run.bold = True
title_run.font.size = Pt(14)
content_run = paragraph.add_run("This is the main content with comprehensive formatting applied.")
content_run.font.size = Pt(12)
doc.save("professional_document.docx")
This creates a justified paragraph. It has spacing and indentation. The title is bold and larger than the content.
Conclusion
Python-docx provides robust paragraph formatting. You can control alignment, spacing, fonts, and indentation.
These tools help create professional documents. They work for reports, letters, and automated document generation.
Remember to explore our Install python-docx for Word Documents in Python guide if you need installation help.
Start with basic formatting. Then experiment with advanced features. Your Word documents will impress everyone.