Last modified: Nov 09, 2025 By Alexander Williams
Set Custom Fonts in docx Using Python
Working with Microsoft Word documents programmatically can save time. The python-docx library makes this easy. This guide shows how to set custom fonts in your documents.
Understanding Font Handling in python-docx
Python-docx provides powerful document manipulation capabilities. Font control is one of its key features. You can change font family, size, color, and style.
The library uses a style-based approach. This maintains document consistency. It also allows for precise formatting control.
Before diving into fonts, you might want to learn about Python-docx Text Styling Guide for comprehensive formatting techniques.
Installing python-docx
First, ensure you have python-docx installed. Use pip for installation.
pip install python-docx
Basic Font Configuration
Let's start with a simple example. We'll create a document and set basic font properties.
from docx import Document
from docx.shared import Pt
from docx.shared import RGBColor
# Create a new document
doc = Document()
# Add a paragraph
paragraph = doc.add_paragraph()
# Add a run (text with same formatting)
run = paragraph.add_run('This text uses custom font styling.')
# Access the font object
font = run.font
# Set font properties
font.name = 'Arial'
font.size = Pt(14)
font.color.rgb = RGBColor(255, 0, 0) # Red color
# Save the document
doc.save('custom_fonts.docx')
This code creates a document with red Arial text. The font object controls all text properties.
Working with Font Families
You can specify any font family installed on the system. The font must be available where the document opens.
from docx import Document
doc = Document()
p = doc.add_paragraph()
# Different font families
run1 = p.add_run('This is in Times New Roman. ')
run1.font.name = 'Times New Roman'
run2 = p.add_run('This is in Calibri. ')
run2.font.name = 'Calibri'
run3 = p.add_run('This is in Courier New.')
run3.font.name = 'Courier New'
doc.save('multiple_fonts.docx')
This example shows multiple fonts in one paragraph. Each run can have different formatting.
Controlling Font Size
Font size is measured in points. Use the Pt class from docx.shared.
from docx import Document
from docx.shared import Pt
doc = Document()
p = doc.add_paragraph()
# Different font sizes
sizes = [Pt(8), Pt(12), Pt(18), Pt(24)]
for i, size in enumerate(sizes):
run = p.add_run(f'Size {int(size.pt)} points. ')
run.font.size = size
doc.save('font_sizes.docx')
The output shows text in increasing sizes. This helps create visual hierarchy.
Font Styles and Effects
Python-docx supports various font styles. These include bold, italic, underline, and more.
from docx import Document
doc = Document()
p = doc.add_paragraph()
# Bold text
run1 = p.add_run('Bold text. ')
run1.font.bold = True
# Italic text
run2 = p.add_run('Italic text. ')
run2.font.italic = True
# Underlined text
run3 = p.add_run('Underlined text. ')
run3.font.underline = True
# Strikethrough
run4 = p.add_run('Strikethrough text.')
run4.font.strike = True
doc.save('font_styles.docx')
Combine these properties for complex formatting. This creates professional-looking documents.
Working with Font Colors
Color control is essential for document design. Use RGBColor for precise color specification.
from docx import Document
from docx.shared import RGBColor
doc = Document()
p = doc.add_paragraph()
# Define some colors
colors = {
'Red': RGBColor(255, 0, 0),
'Green': RGBColor(0, 255, 0),
'Blue': RGBColor(0, 0, 255),
'Purple': RGBColor(128, 0, 128)
}
for color_name, rgb_color in colors.items():
run = p.add_run(f'{color_name} text. ')
run.font.color.rgb = rgb_color
doc.save('font_colors.docx')
This creates text in different colors. Color coding can highlight important information.
Applying Fonts to Document Elements
Font settings apply to various document elements. This includes headings, lists, and tables.
For working with structured content, check out Python-docx Table Creation Best Practices Guide and Python docx Lists: Bulleted and Numbered.
from docx import Document
from docx.shared import Pt
doc = Document()
# Style for headings
heading = doc.add_heading('Document Title', level=1)
heading_runs = heading.runs
for run in heading_runs:
run.font.name = 'Calibri'
run.font.size = Pt(16)
run.font.bold = True
# Style for normal paragraphs
paragraph = doc.add_paragraph('This is normal paragraph text.')
paragraph_runs = paragraph.runs
for run in paragraph_runs:
run.font.name = 'Times New Roman'
run.font.size = Pt(11)
doc.save('styled_elements.docx')
This approach ensures consistent styling. It works across different document sections.
Handling Font Availability
Font availability can be tricky. Documents may display differently on various systems.
Always use common fonts or embed custom ones. Test documents on target systems.
Consider providing fallback fonts. This ensures better compatibility across devices.
Best Practices for Font Management
Follow these practices for better results. They ensure professional and consistent documents.
Define font styles at the beginning. Use consistent sizing throughout your document.
Limit the number of font families. Too many fonts can make documents look unprofessional.
Use font variations purposefully. Create clear visual hierarchy with sizes and weights.
Common Issues and Solutions
Fonts not displaying correctly? This usually means the font isn't available on the system.
Use web-safe fonts for better compatibility. These include Arial, Times New Roman, and Courier New.
For advanced document features, explore Python-docx Headers Footers Guide to complete your document formatting skills.
Conclusion
Setting custom fonts with python-docx is straightforward. The library provides comprehensive font control.
You can customize font family, size, color, and style. This creates professional-looking documents programmatically.
Remember to consider font availability. Test your documents on target systems for best results.
With these techniques, you can generate beautifully formatted Word documents. Automate your document creation workflow effectively.