DEV Community

Allen Yang
Allen Yang

Posted on

Adding and Setting Text Boxes in PowerPoint Slides Using Python

Adding and Setting Text Boxes in PowerPoint Using Python

When creating PowerPoint presentations, text boxes are one of the most commonly used elements. Programmatically adding and configuring text boxes enables batch creation of presentations, automated report generation, and other tasks. This article demonstrates how to add text boxes to PowerPoint using Python and configure properties such as text content, formatting, and margins.

Environment Setup

First, install the Spire.Presentation library:

pip install Spire.Presentation
Enter fullscreen mode Exit fullscreen mode

Basic Text Box Operations

Creating a Presentation with Text Boxes

In PowerPoint, text boxes are actually a special type of shape. By adding a rectangle shape and setting its TextFrame property, text boxes can be created.

from spire.presentation.common import *
from spire.presentation import *

# Create a presentation instance
ppt = Presentation()

# Add a rectangle shape as a text box
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# Set text content
shape.TextFrame.Text = "This is a text box example."

# Save the document
ppt.SaveToFile("TextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The generated PowerPoint presentation is shown below:

Adding text boxes in PowerPoint using Python

In this code, the AppendShape method adds a shape to the slide. RectangleF.FromLTRB specifies the position and size of the shape, with parameters representing left, top, right, and bottom boundary coordinates respectively.

Setting Text Formatting

Text within text boxes can be formatted with font, color, size, and other attributes:

from spire.presentation.common import *
from spire.presentation import *

ppt = Presentation()

# Add a text box
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# Set text content
shape.TextFrame.Text = "PowerPoint Automation"

# Set text formatting
paragraph = shape.TextFrame.Paragraphs[0]
textRange = paragraph.TextRanges[0]

# Set font
textRange.LatinFont = TextFont("Arial")
textRange.FontHeight = 24  # Font size

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

# Set bold
textRange.Format.IsBold = TriState.TTrue

# Set shape border
shape.ShapeStyle.LineColor.Color = Color.get_Black()

ppt.SaveToFile("FormattedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The generated PowerPoint presentation is shown below:

Setting text formatting in PowerPoint using Python

Here, the first paragraph is accessed via TextFrame.Paragraphs[0], and the first text range is accessed via TextRanges[0], then font properties are configured.

Multi-Paragraph Text Boxes

Text boxes can contain multiple paragraphs, each with different formatting:

from spire.presentation.common import *
from spire.presentation import *

ppt = Presentation()

# Add a text box
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 80, 650, 320)
)

# Get TextFrame
tf = shape.TextFrame

# Set the first paragraph
para0 = tf.Paragraphs[0]
para0.Text = "First Paragraph: Title"
para0.TextRanges[0].FontHeight = 20
para0.TextRanges[0].Fill.FillType = FillFormatType.Solid
para0.TextRanges[0].Fill.SolidColor.Color = Color.get_DarkBlue()
para0.TextRanges[0].Format.IsBold = TriState.TTrue

# Add the second paragraph
para1 = TextParagraph()
tf.Paragraphs.Append(para1)
para1.Text = "Second Paragraph: Content"
para1.TextRanges[0].FontHeight = 16
para1.TextRanges[0].Fill.FillType = FillFormatType.Solid
para1.TextRanges[0].Fill.SolidColor.Color = Color.get_Black()

# Add the third paragraph
para2 = TextParagraph()
tf.Paragraphs.Append(para2)
para2.Text = "Third Paragraph: Summary"
para2.TextRanges[0].FontHeight = 14
para2.TextRanges[0].Fill.FillType = FillFormatType.Solid
para2.TextRanges[0].Fill.SolidColor.Color = Color.get_Gray()

ppt.SaveToFile("MultiParagraphTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The generated PowerPoint presentation is shown below:

Adding multi-paragraph text boxes in PowerPoint using Python

New paragraphs are created using TextParagraph(), then added to the text box using the tf.Paragraphs.Append() method.

Setting Text Box Margins

Text box padding controls the distance between text and borders:

from spire.presentation.common import *
from spire.presentation import *

ppt = Presentation()

# Add a text box
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# Set text content
shape.TextFrame.Text = "The text box has appropriate margins, which enhances the visual appearance of the text box content."

# Set text box margins
shape.TextFrame.MarginTop = 15      # Top margin
shape.TextFrame.MarginBottom = 20   # Bottom margin
shape.TextFrame.MarginLeft = 25     # Left margin
shape.TextFrame.MarginRight = 25    # Right margin

# Set paragraph alignment
shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left

# Set shape style
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.get_LightBlue()
shape.ShapeStyle.LineColor.Color = Color.get_Black()

ppt.SaveToFile("TextBoxWithMargins.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The generated PowerPoint presentation is shown below:

Setting text box margins in PowerPoint using Python

Margins are measured in points. Appropriate margin settings enhance the visual appeal of text box content.

Paragraph Alignment and Indentation

Paragraphs within text boxes can be configured with alignment and indentation:

from spire.presentation.common import *
from spire.presentation import *

ppt = Presentation()

# Add a text box
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 80, 650, 350)
)

tf = shape.TextFrame

# First paragraph: left-aligned, no indentation
para0 = tf.Paragraphs[0]
para0.Text = "Left Aligned Text"
para0.Alignment = TextAlignmentType.Left
para0.Indent = 0

# Second paragraph: center-aligned
para1 = TextParagraph()
tf.Paragraphs.Append(para1)
para1.Text = "Center Aligned Text"
para1.Alignment = TextAlignmentType.Center

# Third paragraph: right-aligned
para2 = TextParagraph()
tf.Paragraphs.Append(para2)
para2.Text = "Right Aligned Text"
para2.Alignment = TextAlignmentType.Right

# Fourth paragraph: left-aligned with indentation
para3 = TextParagraph()
tf.Paragraphs.Append(para3)
para3.Text = "Indented Text"
para3.Alignment = TextAlignmentType.Left
para3.Indent = 50  # 50-point indentation

ppt.SaveToFile("TextBoxAlignment.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The generated PowerPoint presentation is shown below:

Setting paragraph alignment and indentation in PowerPoint using Python

The TextAlignmentType enumeration provides various alignment options, including Left, Center, Right, Justify, and others.

Practical Tips

Setting Line Spacing

shape.TextFrame.Paragraphs[0].LineSpacing = 150  # 1.5 line spacing
Enter fullscreen mode Exit fullscreen mode

Line spacing is expressed as a percentage, where 100 represents single spacing, 150 represents 1.5 spacing, and 200 represents double spacing.

Setting Text Direction

shape.TextFrame.Paragraphs[0].TextDirection = TextDirectionType.Vertical
Enter fullscreen mode Exit fullscreen mode

Text can be set to horizontal or vertical orientation for special layout requirements.

Removing Text Boxes

To remove text boxes from slides:

from spire.presentation.common import *
from spire.presentation import *

ppt = Presentation()
ppt.LoadFromFile("input.pptx")

slide = ppt.Slides[0]

# Iterate through and remove all shapes (including text boxes)
i = 0
while i < slide.Shapes.Count:
    shape = slide.Shapes[i] if isinstance(slide.Shapes[i], IAutoShape) else None
    if shape:
        slide.Shapes.Remove(shape)
    else:
        i += 1

ppt.SaveToFile("RemovedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

Summary

This article has demonstrated fundamental methods for adding and configuring text boxes in PowerPoint using Python, including creating text boxes, setting text formatting, adding multi-paragraph content, configuring margins and alignment. These techniques enable automated presentation generation, improving workflow efficiency. In practical applications, these features can be combined to create presentation templates that meet specific requirements.

Top comments (0)