商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > Python操作PowerPoint实现添加与设置文本框完整教程

Python操作PowerPoint实现添加与设置文本框完整教程

  发布于2026-07-15 阅读(0)

扫一扫,手机访问

要说 PowerPoint 里最常用的元素,文本框绝对排得上号。如果只是手动做几页,拖拽一下自然没问题,但一旦涉及批量创建演示文稿、自动化报告生成这类任务,用代码来搞定就会顺手得多。这篇文章就聊聊怎么用 Python 在 PowerPoint 里添加文本框,以及如何设置文本内容、格式、边距等属性,都是实际开发中会碰到的操作。

环境配置

开始之前,需要先装一个叫 Spire.Presentation 的库:

pip install Spire.Presentation

基本文本框操作

创建包含文本框的演示文稿

其实在 PowerPoint 底层,文本框就是一种特殊的形状(Shape)。方式很简单:先添加一个矩形形状,然后通过它的 TextFrame 属性来设置文本内容。

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

# 创建演示文稿实例
ppt = Presentation()

# 添加一个矩形形状作为文本框
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# 设置文本内容
shape.TextFrame.Text = "这是一个示例文本框"

# 保存文档
ppt.Sa veToFile("TextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()

生成的 PowerPoint 演示文稿如下:

Python操作PowerPoint实现添加与设置文本框完整教程

这段代码里,AppendShape 负责在幻灯片上添加形状,RectangleF.FromLTRB 则指定形状的位置和大小,四个参数依次是左、上、右、下边界的坐标。

设置文本格式

文本框里的文字当然可以控制字体、颜色、大小——下面是一个比较完整的例子:

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

ppt = Presentation()

# 添加文本框
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# 设置文本内容
shape.TextFrame.Text = "PowerPoint 自动化处理"

# 设置文本格式
paragraph = shape.TextFrame.Paragraphs[0]
textRange = paragraph.TextRanges[0]

# 设置字体
textRange.LatinFont = TextFont("微软雅黑")
textRange.FontHeight = 24  # 字体大小

# 设置字体颜色
textRange.Fill.FillType = FillFormatType.Solid
textRange.Fill.SolidColor.Color = Color.get_Red()

# 设置加粗
textRange.Format.IsBold = TriState.TTrue

# 设置形状边框
shape.ShapeStyle.LineColor.Color = Color.get_Black()

ppt.Sa veToFile("FormattedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()

生成的 PowerPoint 演示文稿如下:

Python操作PowerPoint实现添加与设置文本框完整教程

关键点在于通过 TextFrame.Paragraphs[0] 拿到第一个段落,再通过 TextRanges[0] 拿到第一个文本范围,后续所有字体属性的设置都基于这个对象。

多段落文本框

很多时候文本框里不只有一段文字,多个段落还能各自拥有不同的格式:

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

ppt = Presentation()

# 添加文本框
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 80, 650, 320)
)

# 获取 TextFrame
tf = shape.TextFrame

# 设置第一个段落
para0 = tf.Paragraphs[0]
para0.Text = "第一段:标题文本"
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

# 添加第二个段落
para1 = TextParagraph()
tf.Paragraphs.Append(para1)
para1.Text = "第二段:正文内容"
para1.TextRanges[0].FontHeight = 16
para1.TextRanges[0].Fill.FillType = FillFormatType.Solid
para1.TextRanges[0].Fill.SolidColor.Color = Color.get_Black()

# 添加第三个段落
para2 = TextParagraph()
tf.Paragraphs.Append(para2)
para2.Text = "第三段:补充说明"
para2.TextRanges[0].FontHeight = 14
para2.TextRanges[0].Fill.FillType = FillFormatType.Solid
para2.TextRanges[0].Fill.SolidColor.Color = Color.get_Gray()

ppt.Sa veToFile("MultiParagraphTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()

生成的 PowerPoint 演示文稿如下:

Python操作PowerPoint实现添加与设置文本框完整教程

思路很清晰:新建 TextParagraph() 对象,然后用 tf.Paragraphs.Append() 追加到文本框,每个段落单独设置格式。

设置文本框边距

文本框的内边距直接控制文本和边框之间的距离,调好了视觉效果会舒服很多:

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

ppt = Presentation()

# 添加文本框
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 100, 500, 250)
)

# 设置文本内容
shape.TextFrame.Text = "设置了内边距的文本框,文本与边框之间有合适的间距。"

# 设置文本框内边距
shape.TextFrame.MarginTop = 15      # 上边距
shape.TextFrame.MarginBottom = 20   # 下边距
shape.TextFrame.MarginLeft = 25     # 左边距
shape.TextFrame.MarginRight = 25    # 右边距

# 设置段落对齐方式
shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left

# 设置形状样式
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.get_LightBlue()
shape.ShapeStyle.LineColor.Color = Color.get_Black()

ppt.Sa veToFile("TextBoxWithMargins.pptx", FileFormat.Pptx2013)
ppt.Dispose()

生成的 PowerPoint 演示文稿如下:

Python操作PowerPoint实现添加与设置文本框完整教程

边距的单位是磅(point),数值根据实际版式调整就好。

段落对齐和缩进

除了边距,段落本身的对齐方式和缩进也很常用:

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

ppt = Presentation()

# 添加文本框
shape = ppt.Slides[0].Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(50, 80, 650, 350)
)

tf = shape.TextFrame

# 第一段:左对齐,无缩进
para0 = tf.Paragraphs[0]
para0.Text = "左对齐文本"
para0.Alignment = TextAlignmentType.Left
para0.Indent = 0

# 第二段:居中对齐
para1 = TextParagraph()
tf.Paragraphs.Append(para1)
para1.Text = "居中对齐文本"
para1.Alignment = TextAlignmentType.Center

# 第三段:右对齐
para2 = TextParagraph()
tf.Paragraphs.Append(para2)
para2.Text = "右对齐文本"
para2.Alignment = TextAlignmentType.Right

# 第四段:左对齐,带缩进
para3 = TextParagraph()
tf.Paragraphs.Append(para3)
para3.Text = "缩进文本示例"
para3.Alignment = TextAlignmentType.Left
para3.Indent = 50  # 缩进50磅

ppt.Sa veToFile("TextBoxAlignment.pptx", FileFormat.Pptx2013)
ppt.Dispose()

生成的 PowerPoint 演示文稿如下:

Python操作PowerPoint实现添加与设置文本框完整教程

TextAlignmentType 枚举提供了 LeftCenterRightJustify 等选项,按需选用即可。

实用技巧

设置行间距

shape.TextFrame.Paragraphs[0].LineSpacing = 150  # 1.5倍行距

行间距以百分比表示,100 是单倍行距,150 就是 1.5 倍,200 就是双倍行距,很直观。

设置文本方向

shape.TextFrame.Paragraphs[0].TextDirection = TextDirectionType.Vertical

想要文字竖排?直接改成 Vertical 就行,适合某些特殊版式。

移除文本框

如果反过来需要从幻灯片中删掉某个文本框,可以这样操作:

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

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

slide = ppt.Slides[0]

# 遍历并移除所有形状(包括文本框)
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.Sa veToFile("RemovedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()

总结

从创建文本框、设置文本格式,到添加多段落、调整边距和对齐方式,这些基本操作组合起来,基本能覆盖大部分自动化生成 PPT 的需求。实际项目中可以根据具体场景灵活搭配,打造属于自己的模板。希望这篇文章能帮你省下一些手动排版的时间。

本文转载于:https://www.jb51.net/python/362654rz8.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注