How to Change Certain Fonts in Word Documents with Python: Quick Tips
By default, Microsoft Word uses the Calibri font for new documents. However, in professional or creative documents, making points stand out often requires using a different font alongside font styles.
Manually adjusting fonts can be tedious and impractical, especially when working with lengthy reports or bulk documents. This article will show you how to change certain fonts in Word with Python, offering a fast and efficient solution ideal for tasks like reformatting business reports or customizing large documents with consistent styling.
Python Library to Change Fonts in Word Files
Many Word document editors allow users to change fonts, such as MS Word, WPS, and so on. When it comes to Python libraries, popular options like Spire, Aspose, and python-docx may come to mind. Among these, Spire.Doc for Python (full name) stands out for its user-friendly approach and highly responsive technical support, making it the recommended choice in this article. In the following sections, we’ll demonstrate its capabilities. That said, the steps for working with most Python libraries are generally similar.
You can install it using the pip command: pip install Spire.Doc
.
How to Change Certain Fonts of Paragraphs in Word Documents
Choosing appropriate fonts significantly enhances the readability and visual appeal of your content. When a paragraph serves as a comment or differs from the main text, adjusting its font can help it stand out. By calling the Document.Styles.Add() and Paragraph.ApplyStyle() methods, you can easily customize the font and style of a paragraph to suit your needs.
Steps to change paragraphs’ fonts in Word files:
Create an object of the Document class, and use the Document.LoadFromFile() method to read a Word document from the local storage.
Get a specific section using the Document.Section[] property.
Get a paragraph with the Section.Paragrphs[] property.
Create a ParagraphStyle instance, and customize the font through properties under it.
Add the style to the document with the Document.Styles.Add() method.
Apply the font style to the paragraph using the Paragraph.ApplyStyle() method.
Save the updated Word document with the Document.SaveToFile() method.
Here is the code example of changing the font of the 4th paragraph of the first section in a Word file:
from spire.doc import *
from spire.doc.common import *
# Create a Document instance
document = Document()
# Load a Word document
document.LoadFromFile("/sample.docx")
# Get the first section
section = document.Sections[0]
# Get a specific paragraph
paragraph = section.Paragraphs[3]
# Create a paragraph style
style = ParagraphStyle(document)
style.Name = 'NewStyle'
style.CharacterFormat.Bold = True
style.CharacterFormat.Italic = True
style.CharacterFormat.TextColor = Color.get_Green()
style.CharacterFormat.FontName = 'Cambria'
document.Styles.Add(style)
# Apply the style to the paragraph
paragraph.ApplyStyle(style.Name)
# Save the result document
document.SaveToFile("/ChangeFontOfParagraph.docx", FileFormat.Docx)
# Close the document
document.Close()
How to Change Certain Fonts of Specified Text in Word Files
If you want to change the font of specific text rather than the entire paragraph, you can use the Document.FindAllString() method to locate the matching text and then apply the desired font. Let’s walk through the detailed steps.
Steps to change fonts of specified text in Word documents:
Create a Document instance, and use the Document.LoadFromFile() method to load a Word file.
Find the text to change fonts using the Document.FindAllString() method.
Loop through all finding text and customize the font through properties under the TextSelection.GetAsOneRange.CharacterFormat object.
Save the modified Word document with the Document.SaveToFile() method.
Here is the code example of changing the font of the text “AI-generated art”:
from spire.doc import *
from spire.doc.common import *
# Create a Document instance
document = Document()
# Load a Word document
document.LoadFromFile("/sample.docx")
# Find the text that you want to change font
textSelections = document.FindAllString('AI-generated art', False, True)
# Change the font style of the text
for selection in textSelections:
selection.GetAsOneRange().CharacterFormat.TextColor = Color.get_Green()
selection.GetAsOneRange().CharacterFormat.Bold = True
selection.GetAsOneRange().CharacterFormat.FontName = "Segoe Print"
# Save the result document
document.SaveToFile("/ChangeFontOfText.docx", FileFormat.Docx)
# Close the document
document.Close()
The Conclusion
This page explains how to change certain fonts in Word files, covering both paragraph fonts and specific text fonts. Each section includes detailed instructions and code examples for your reference. By the end of this article, you'll see how quick and easy it is to manage fonts in Word documents!