Python: Set or Change Fonts in PowerPoint

2023-11-30 03:00:34 Written by  support iceblue
Rate this item
(0 votes)

In a PowerPoint document, the choice of fonts plays a significant role in enhancing the overall visual appeal and effectiveness of the presentation. Different fonts can be used to establish a visual hierarchy, allowing you to emphasize key points, headings, or subheadings in your presentation and guide the audience's attention. This article introduces how to set or change fonts in a PowerPoint document in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Set Fonts when Creating a New PowerPoint Document in Python

Spire.Presentation for Python offers the TextRange class to represent a range of text. A paragraph can consist of one or more text ranges. To apply font formatting to the characters in a text range, you can use the properties like LatinFont, IsBold, IsItalic, and FontHeight of the TextRange class. The following are the steps to set fonts when creating a new PowerPoint document in Python.

  • Create a Presentation object.
  • Get the first slide through Presentation.Slides[0] property.
  • Add a shape to the slide using ISlide.Shapes.AppendShape() method.
  • Add text to the shape using IAutoShape.AppendTextFrame() method.
  • Get TextRange object through IAutoShape.TextFrame.TextRange property.
  • Set the font information such as font name, font size, bold, italic, underline, and text color through the properties under the TextRange object.
  • Save the presentation to a PPTX file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
import math
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()

# Set slide size type
presentation.SlideSize.Type = SlideSizeType.Screen16x9

# Add a shape to the first slide
rec = RectangleF.FromLTRB (30, 100, 900, 250)
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rec)

# Set line color and fill type of the shape
shape.ShapeStyle.LineColor.Color = Color.get_Transparent()
shape.Fill.FillType = FillFormatType.none

# Add text to the shape
shape.AppendTextFrame("Spire.Presentation for Python is a professional presentation processing API that \
is highly compatible with PowerPoint. It is a completely independent class library that developers can \
use to create, edit, convert, and save PowerPoint presentations efficiently without installing Microsoft PowerPoint.")

# Get text of the shape as a text range
textRange = shape.TextFrame.TextRange

# Set font name
textRange.LatinFont = TextFont("Times New Roman")

# Set font style (bold & italic)
textRange.IsBold = TriState.TTrue
textRange.IsItalic = TriState.TTrue

# Set underline type
textRange.TextUnderlineType = TextUnderlineType.Single

# Set font size
textRange.FontHeight = 22

# Set text color
textRange.Fill.FillType = FillFormatType.Solid
textRange.Fill.SolidColor.Color = Color.get_CadetBlue()

# Set alignment
textRange.Paragraph.Alignment = TextAlignmentType.Left

# Set line spacing
textRange.LineSpacing = 0.5

# Save to file
presentation.SaveToFile("output/SetFont.pptx", FileFormat.Pptx2019)
presentation.Dispose()

Python: Set or Change Fonts in PowerPoint

Change Fonts in an Existing PowerPoint Document in Python

To change the font for a specific paragraph, we need to get the paragraph from the document. Then, iterate through the text ranges in the paragraph and reset the font information for each text range. Below are the steps to change the font of a paragraph in an existing PowerPoint document using Spire.Presentation for Python.

  • Create a Presentation object.
  • Get a specific slide through Presentation.Slides[index] property.
  • Get a specific shape through ISlide.Shapes[index] property.
  • Get a specific paragraph of the shape through IAutoShape.TextFrame.Paragraphs[index] property.
  • Iterate through the text ranges in the paragraph.
  • Set the font information such as font name, font size, bold, italic, underline, and text color of a specific text range through the properties under the TextRange object.
  • Save the presentation to a PPTX file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object 
presentation = Presentation()

# Load a PowerPoint file
presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx")

# Get the first slide
slide = presentation.Slides[0]

# Get the first shape on the slide
shape = slide.Shapes[0]

# Get the first paragraph of the shape
paragraph = shape.TextFrame.Paragraphs[0]

# Create a font
newFont = TextFont("Times New Roman")

# Loop through the text ranges in the paragraph
for textRange in paragraph.TextRanges:

    # Apply font to a specific text range
    textRange.LatinFont = newFont

    # Set font to Italic
    textRange.Format.IsItalic = TriState.TTrue

    # Set font size
    textRange.FontHeight = 25

    # Set font color
    textRange.Fill.FillType = FillFormatType.Solid
    textRange.Fill.SolidColor.Color = Color.get_Purple()

# Save to file
presentation.SaveToFile("output/ChangeFont.pptx", FileFormat.Pptx2019)
presentation.Dispose()

Python: Set or Change Fonts in PowerPoint

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.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:21