Spire.Office Knowledgebase Page 63 | E-iceblue

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.

Setting the background colors for paragraphs and text in Word documents can significantly enhance the presentation and readability of content. Customizing background color is an effective approach to emphasize key information, categorize content, and add a personalized touch, thereby making it easy to create polished and professional documents. By carefully selecting and applying background colors, documents can be transformed into visually appealing works that effectively convey information and engage the reader. This article shows how to use Spire.Doc for Python to set background colors for paragraphs and text in Word documents, unlocking new possibilities for document styling and customization.

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

Set Background Color for Paragraphs Using Python

By using Spire.Doc for Python, developers can get any paragraph in any section. After getting a paragraph, developers can apply a background color to it by assigning a Color object to Paragraph.Format.BackColor property. Below are the detailed steps:

  • Create an instance of Document class and load a Word document using Document.LoadFromFile() method.
  • Get a section using Document.Sections.get_Item() method.
  • Get a paragraph in the section using Section.Paragraphs.get_Item() method.
  • Set the background color of the paragraph through Paragraph.Format.BackColor property.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of Document class and load a Word document
doc = Document()
doc.LoadFromFile("Sample.docx")

# Get the first section
section = doc.Sections.get_Item(0)

# Get the fifth paragraph
paragraph = section.Paragraphs.get_Item(4)

# Set background color for the paragraph
paragraph.Format.BackColor = Color.get_DarkGreen()

# Save the document
doc.SaveToFile("output/ParagraphBackground.docx")
doc.Close()

Python: Set Background Colors for Word Paragraphs or Text

Set Background Colors for Selected Text Using Python

Spire.Doc for Python enables developers to find all the occurrences of specific text in a Word document with Document.FindAllString() method. After getting the finding results, developers can set the background for them through TextRange.CharacterFormat.TextBackgroundColor property. The detailed steps are as follows:

  • Create an instance of Document class and load a Word document using Document.LoadFromFile() method.
  • Find all the occurrences of specific text using Document.FindAllString() method.
  • Loop through the occurrences, get each occurrence as a text range using TextSelection.GetAsOneRange(True) method, and set the background color of each occurrence through TextRange.CharacterFormat.TextBackgroundColor property. It is also possible to get only one occurrence from the result list and set the background color for the occurrence.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of Document class and load a Word document
doc = Document()
doc.LoadFromFile("Sample.docx")

# Find text in the Word document
findResults = doc.FindAllString("advantages of LCD screens", False, False)

# Loop through the finding results to set background color for all occurrences
for text in findResults:
    # Get an occurrence as a text range
    textRange = text.GetAsOneRange(True)
    # Set the background color of the text range
    textRange.CharacterFormat.TextBackgroundColor = Color.get_LightCoral()

# Set the background color of a sepecified occurrence
# Get an occurrence as one text range
# textRange = findResults[1].GetAsOneRange()
# Set the background color of the text range
# textRange.CharacterFormat.BackgroundColor = Color.get_DarkCyan()

# Save the document
doc.SaveToFile("output/TextBackground.docx")
doc.Close()

Python: Set Background Colors for Word Paragraphs or Text

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.

To get the number of pages in a PDF file, you can open the file in a PDF viewer such as Adobe, which has a built-in page count feature. However, when there is a batch of PDF files, opening each file to check how many pages it contains is a time-consuming task. In this article, you will learn how to quicky count the number of pages in a PDF file through programming using Spire.PDF for Python.

Install Spire.PDF for Python

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

pip install Spire.PDF

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

Count the Number of Pages in a PDF File in Python

Spire.PDF for Python offers the PdfDocument.Pages.Count property to quickly count the number of pages in a PDF file without opening it. The following are the detailed steps.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Count the number of pages in the PDF document using PdfDocument.Pages.Count property.
  • Write the result to a TXT file or print it out directly.
  • Python
from spire.pdf.common import *
from spire.pdf import *

def AppendText(fname: str, text: str):
    fp = open(fname, "w")
    fp.write(text + "\n")
    fp.close()

# Specify the input and output files
inputFile = "contract.pdf"
outputFile = "GetNumberOfPages.txt"

# Create a PdfDocument object
pdf = PdfDocument()

# Load a PDF document from disk
pdf.LoadFromFile(inputFile)

# Count the number of pages in the document
count = pdf.Pages.Count
# Print the result
print("Total Pages:", count)

# Write the result to a TXT file
AppendText(
    outputFile, "The number of pages in the pdf document is: " + str(count))
pdf.Close()

Python: Count the Number of Pages in a PDF File

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.

page 63

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details