Python: Change the Font of a Word Document
Modifying the font in a Word document can significantly impact its visual appearance and overall readability. Whether you want to enhance the document's style or align it with specific formatting requirements, changing the font is a straightforward process that allows you to customize your text. In this article, you will learn how to change font of a paragraph or a piece of text in a Word document in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Change the Font of a Paragraph in Python
Using Spire.Doc for Python, you can create a ParagraphStyle object which defines the font information that can be applied to a certain paragraph. The following are the steps to change the font of a paragraph.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section through Document.Sections[index] property.
- Get a specified paragraph that you want to change the font through Section.Paragraphs[index] property.
- Create a ParagraphStyle instance, specifying the font name, font color and font style through the properties under it.
- Add the style to the document using Document.Styles.Add() method.
- Apply the style to the paragraph using Paragraph.ApplyStyle() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document instance document = Document() # Load a Word document document.LoadFromFile('C:/Users/Administrator/Desktop/input.docx') # Get the first section section = document.Sections[0] # Get a specific paragraph paragraph = section.Paragraphs[2] # Create a paragraph style style = ParagraphStyle(document) style.Name = 'NewStyle' style.CharacterFormat.Bold = True style.CharacterFormat.Italic = True style.CharacterFormat.TextColor = Color.get_Red() style.CharacterFormat.FontName = 'Cambria' document.Styles.Add(style) # Apply the style to the paragraph paragraph.ApplyStyle(style.Name) # Save the result document document.SaveToFile('output/ChangeFontOfParagraph.docx', FileFormat.Docx)
Change the Font of Specific Text in Python
To change the font of specific text (letter, phrase or sentence) in a Word document, you need first to find the text from the document and then set a different color or font style for it. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Find the text that you want to change font color using Document.FindAllString() method.
- Loop through all occurrences of the searched text and change the font color or style for each occurrence through the properties under TextSelection.GetAsOneRange().CharacterFormat object.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document instance document = Document() # Load a Word document document.LoadFromFile('C:/Users/Administrator/Desktop/input.docx') # Find the text that you want to change font textSelections = document.FindAllString('programming language', False, True) # Change the font style of the text for selection in textSelections: selection.GetAsOneRange().CharacterFormat.TextColor = Color.get_Red() selection.GetAsOneRange().CharacterFormat.Bold = True # Save the result document document.SaveToFile('output/ChangeFontOfText.docx', FileFormat.Docx)
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.