Last modified: Nov 10, 2025 By Alexander Williams

Python docx Inline Formatting: Bold, Italic, Underline

Python docx is a powerful library. It helps create and modify Word documents. One key feature is inline text formatting. This guide covers bold, italic, and underline formatting.

Getting Started with python-docx

First, install the python-docx library. Use pip for installation. This library requires Python 3.6 or higher.


pip install python-docx

After installation, import the library in your script. The main module is docx. You will work with Document objects.

 
from docx import Document

Basic Document Setup

Create a new document object. Add a paragraph to work with. Paragraphs contain runs of text.

 
# Create a new document
doc = Document()

# Add a paragraph
paragraph = doc.add_paragraph('This is a sample text.')

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

Understanding Runs and Formatting

Text in paragraphs is divided into runs. A run is a sequence of characters with the same formatting. You can apply formatting to entire runs.

To format specific parts of text, you need to create separate runs. Each run can have its own formatting properties. This gives you fine control over text appearance.

Bold Text Formatting

Apply bold formatting using the bold property. Set it to True for bold text. Set it to False for normal weight.

 
# Create document and paragraph
doc = Document()
paragraph = doc.add_paragraph()

# Add a run and make it bold
bold_run = paragraph.add_run('This text is bold.')
bold_run.bold = True

doc.save('bold_text.docx')

The text "This text is bold." will appear in bold. You can combine bold with other formatting. This creates rich text documents.

Italic Text Formatting

Italic formatting uses the italic property. Set it to True for italic text. This slants the characters for emphasis.

 
# Create document
doc = Document()
paragraph = doc.add_paragraph()

# Add italic text
italic_run = paragraph.add_run('This text is italic.')
italic_run.italic = True

doc.save('italic_text.docx')

The italic text appears slanted. Use italics for emphasis or foreign words. It works well with other formatting options.

Underline Text Formatting

Underline formatting uses the underline property. Set it to True for underlined text. This adds a line below the text.

 
# Create document
doc = Document()
paragraph = doc.add_paragraph()

# Add underlined text
underline_run = paragraph.add_run('This text is underlined.')
underline_run.underline = True

doc.save('underlined_text.docx')

The text will have a line underneath. Underlining is useful for highlighting. It draws attention to important content.

Combining Multiple Formats

You can combine bold, italic, and underline. Apply multiple properties to the same run. This creates complex formatting.

 
# Create document
doc = Document()
paragraph = doc.add_paragraph()

# Add text with multiple formats
formatted_run = paragraph.add_run('Bold, Italic, and Underlined!')
formatted_run.bold = True
formatted_run.italic = True
formatted_run.underline = True

doc.save('combined_formatting.docx')

The text will be bold, italic, and underlined. This is useful for headings or important notes. You can mix and match as needed.

Formatting Specific Text Parts

Often you need to format only parts of a paragraph. Create multiple runs with different formatting. This gives you precise control.

 
# Create document
doc = Document()
paragraph = doc.add_paragraph()

# Add normal text
paragraph.add_run('This is normal text. ')

# Add bold text
bold_part = paragraph.add_run('This is bold. ')
bold_part.bold = True

# Add italic text
italic_part = paragraph.add_run('This is italic.')
italic_part.italic = True

doc.save('mixed_formatting.docx')

Each part has its own formatting. The paragraph flows naturally. The formatting changes mid-sentence.

Working with Existing Documents

You can also modify existing documents. Load the document and access its paragraphs. Then modify the runs as needed.

 
# Load existing document
doc = Document('existing_document.docx')

# Get first paragraph
paragraph = doc.paragraphs[0]

# Clear existing runs (optional)
paragraph.clear()

# Add new formatted content
paragraph.add_run('New formatted content: ')
bold_text = paragraph.add_run('important!')
bold_text.bold = True

doc.save('modified_document.docx')

This approach is useful for dynamic report templates in Python with docx. You can update documents programmatically.

Common Formatting Patterns

Here are some common formatting patterns. They show practical applications. Use them as starting points for your projects.

 
# Create document with various formatting patterns
doc = Document()

# Heading with bold
heading = doc.add_paragraph()
heading_run = heading.add_run('Document Title')
heading_run.bold = True

# Important note with italic
note = doc.add_paragraph()
note_run = note.add_run('Note: ')
note_run.italic = True
note.add_run('This is important information.')

# Key term with underline
term = doc.add_paragraph()
term.add_run('Key term: ')
underlined_term = term.add_run('critical concept')
underlined_term.underline = True

doc.save('formatting_patterns.docx')

Troubleshooting Formatting Issues

Sometimes formatting doesn't work as expected. Check that you're modifying the correct run. Ensure the property is set correctly.

Common issues include forgetting to set properties. Or applying formatting to the wrong run. Double-check your code logic.

For more help, see our guide on fix common python-docx errors. It covers many common problems and solutions.

Best Practices for Text Formatting

Use formatting consistently throughout your document. Too much formatting can be distracting. Use it to enhance readability.

Plan your formatting strategy before coding. Consider using styles for consistent formatting. This makes maintenance easier.

When working with complex documents, consider Python-docx performance tips for faster document generation. This helps with large documents.

Conclusion

Python docx provides excellent text formatting capabilities. You can apply bold, italic, and underline formatting easily. Use runs to control text appearance.

Remember to work with runs for inline formatting. Combine formats for emphasis. Test your code with various scenarios.

With these techniques, you can create professional-looking documents. Your Word documents will have precise formatting. This enhances their visual appeal and readability.