Last modified: Nov 08, 2025 By Alexander Williams

Python-docx Text Styling Guide

Python-docx makes Word document automation easy. You can create professional reports with styled text. This guide covers text formatting techniques.

Text styling improves document readability. It highlights important information. Proper formatting makes your documents look professional.

Getting Started with python-docx

First, install the python-docx library. Use pip for installation. This library works with Python 3.6 and above.


pip install python-docx

After installation, import the module. Create a new document object. You can now add and style text content.


from docx import Document

# Create a new document
doc = Document()

# Add a paragraph
paragraph = doc.add_paragraph('Hello, World!')

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

This basic example creates a simple document. The text appears with default formatting. Now let's explore text styling options.

Basic Text Formatting

Python-docx provides extensive font formatting. You can change font name, size, and color. Bold, italic, and underline effects are also available.

Access the font property of a run object. A run is a text segment with consistent formatting. Apply styles to individual runs within paragraphs.


from docx import Document
from docx.shared import Pt, RGBColor

doc = Document()
paragraph = doc.add_paragraph()

# Add text with different formatting
run1 = paragraph.add_run('This is bold and blue text')
run1.bold = True
run1.font.color.rgb = RGBColor(0, 0, 255)

run2 = paragraph.add_run(' This is italic and red text')
run2.italic = True
run2.font.color.rgb = RGBColor(255, 0, 0)

run3 = paragraph.add_run(' This is large text')
run3.font.size = Pt(20)

doc.save('formatted_text.docx')

The code creates text with multiple formats. Each run maintains its own styling. This allows rich text within single paragraphs.

Font Properties and Styles

Control various font aspects with python-docx. Set font family, size, and color. Apply text effects like strikethrough and shadow.

Font size uses points measurement. Import Pt from docx.shared. Set precise font sizes for professional results.


from docx import Document
from docx.shared import Pt, Inches
from docx.shared import RGBColor

doc = Document()
paragraph = doc.add_paragraph()

# Comprehensive font formatting example
run = paragraph.add_run('Formatted Text Example')
run.font.name = 'Arial'
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(100, 150, 200)
run.font.bold = True
run.font.italic = False
run.font.underline = True
run.font.strike = False
run.font.shadow = True

doc.save('font_properties.docx')

This example demonstrates multiple font properties. Experiment with different combinations. Find the perfect style for your documents.

Working with Paragraph Formatting

Paragraph formatting controls text alignment and spacing. Set left, right, center, or justified alignment. Control line spacing and indentation.

Access paragraph formatting through the paragraph_format property. This affects the entire paragraph content. Combine with run formatting for complete control.


from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches

doc = Document()

# Center aligned paragraph
paragraph1 = doc.add_paragraph('This paragraph is centered')
paragraph1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Right aligned with indentation
paragraph2 = doc.add_paragraph('This paragraph is right aligned with indentation')
paragraph2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
paragraph2.paragraph_format.left_indent = Inches(0.5)

# Justified alignment with spacing
paragraph3 = doc.add_paragraph('This paragraph has justified alignment and line spacing.')
paragraph3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
paragraph3.paragraph_format.line_spacing = 1.5

doc.save('paragraph_formatting.docx')

Proper paragraph formatting improves document flow. It makes text easier to read. Use alignment consistently throughout your document.

Using Built-in Styles

Python-docx supports Word's built-in styles. Apply heading styles, normal text, and list formats. Consistent styling maintains document professionalism.

Apply styles using the style property. Use predefined style names like 'Heading 1', 'Title', or 'List Bullet'. This ensures consistency across documents.


from docx import Document

doc = Document()

# Apply built-in styles
title = doc.add_paragraph('Document Title', style='Title')
heading1 = doc.add_paragraph('Main Heading', style='Heading 1')
heading2 = doc.add_paragraph('Sub Heading', style='Heading 2')
normal = doc.add_paragraph('This is normal text', style='Normal')

# List styles
doc.add_paragraph('First list item', style='List Bullet')
doc.add_paragraph('Second list item', style='List Bullet')
doc.add_paragraph('Third list item', style='List Bullet')

doc.save('builtin_styles.docx')

Built-in styles save time and ensure consistency. They work well with Word's navigation pane. Readers can easily navigate styled documents.

Advanced Text Effects

Create sophisticated text effects with python-docx. Add highlights, change character spacing, and apply text effects. These features make important text stand out.

Text highlighting uses RGB colors. Character spacing adjusts text density. Combine effects for emphasis where needed.


from docx import Document
from docx.shared import RGBColor, Pt

doc = Document()
paragraph = doc.add_paragraph()

# Text with highlight effect
run1 = paragraph.add_run('Highlighted text ')
run1.font.highlight_color = RGBColor(255, 255, 0)  # Yellow highlight

# Text with character spacing
run2 = paragraph.add_run('Spaced text ')
run2.font.spacing = Pt(2)  # Increased character spacing

# All caps effect
run3 = paragraph.add_run('ALL CAPS TEXT')
run3.font.all_caps = True

# Small caps effect
run4 = paragraph.add_run(' Small Caps Text')
run4.font.small_caps = True

doc.save('advanced_effects.docx')

Advanced effects should be used sparingly. They work best for emphasis. Too many effects can reduce readability.

Combining Formatting Techniques

Real-world documents combine multiple formatting techniques. Use paragraph styles with custom font formatting. Create professional reports automatically.

This example shows a complete formatted section. It uses headings, custom fonts, and paragraph alignment. The result looks professionally designed.


from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = Document()

# Title section
title = doc.add_paragraph('Quarterly Sales Report', style='Title')
title.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

# Header with custom formatting
header = doc.add_paragraph()
header_run = header.add_run('Executive Summary')
header_run.font.name = 'Calibri'
header_run.font.size = Pt(16)
header_run.font.color.rgb = RGBColor(0, 100, 0)
header_run.bold = True

# Content with mixed formatting
content = doc.add_paragraph()
content_run1 = content.add_run('Sales increased by ')
content_run2 = content.add_run('15%')
content_run2.bold = True
content_run2.font.color.rgb = RGBColor(255, 0, 0)
content_run3 = content.add_run(' this quarter. This represents significant growth.')

doc.save('professional_report.docx')

Combined formatting creates visually appealing documents. It guides the reader's attention. Important information stands out clearly.

Best Practices for Text Styling

Follow these best practices for optimal results. Maintain consistency across your documents. Use styles strategically for different content types.

Be consistent with font choices. Use 2-3 fonts maximum per document. Establish a clear hierarchy with heading styles.

Use color purposefully. Highlight key information only. Ensure sufficient contrast for readability.

Combine python-docx with other features. Learn about Python-docx Headers Footers Guide for complete documents. Explore Python-docx Table Formatting Complete Guide for data presentation.

For comprehensive document creation, check the Python-docx Tutorial: Create Word Documents. It covers all essential features.

Conclusion

Python-docx provides powerful text styling capabilities. You can create professional Word documents programmatically. Text formatting makes your documents clear and engaging.

Start with basic font properties. Progress to advanced text effects. Combine techniques for best results.

Remember to maintain consistency. Use styles strategically. Your automated documents will look professionally designed.

Experiment with different formatting options. Create templates for repeated use. Python-docx makes document automation accessible and effective.