Python操作PowerPoint實(shí)現(xiàn)添加與設(shè)置文本框完整教程
在制作 PowerPoint 演示文稿時,文本框是最常用的元素之一。通過編程方式添加和設(shè)置文本框,可以實(shí)現(xiàn)批量創(chuàng)建演示文稿、自動化報告生成等任務(wù)。本文將介紹如何使用 Python 在 PowerPoint 中添加文本框,并設(shè)置文本內(nèi)容、格式和邊距等屬性。
環(huán)境配置
首先需要安裝 Spire.Presentation 庫:
pip install Spire.Presentation
基本文本框操作
創(chuàng)建包含文本框的演示文稿
在 PowerPoint 中,文本框?qū)嶋H上是一種特殊的形狀(Shape)。通過添加矩形形狀并設(shè)置其 TextFrame 屬性,可以創(chuàng)建文本框。
from spire.presentation.common import *
from spire.presentation import *
# 創(chuàng)建演示文稿實(shí)例
ppt = Presentation()
# 添加一個矩形形狀作為文本框
shape = ppt.Slides[0].Shapes.AppendShape(
ShapeType.Rectangle,
RectangleF.FromLTRB(50, 100, 500, 250)
)
# 設(shè)置文本內(nèi)容
shape.TextFrame.Text = "這是一個示例文本框"
# 保存文檔
ppt.SaveToFile("TextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
生成的 PowerPoint 演示文稿如下:

在這段代碼中,AppendShape 方法用于在幻燈片上添加形狀。RectangleF.FromLTRB 指定了形狀的位置和大小,參數(shù)依次為左、上、右、下邊界的坐標(biāo)。
設(shè)置文本格式
文本框中的文本可以設(shè)置字體、顏色、大小等格式屬性:
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)
)
# 設(shè)置文本內(nèi)容
shape.TextFrame.Text = "PowerPoint 自動化處理"
# 設(shè)置文本格式
paragraph = shape.TextFrame.Paragraphs[0]
textRange = paragraph.TextRanges[0]
# 設(shè)置字體
textRange.LatinFont = TextFont("微軟雅黑")
textRange.FontHeight = 24 # 字體大小
# 設(shè)置字體顏色
textRange.Fill.FillType = FillFormatType.Solid
textRange.Fill.SolidColor.Color = Color.get_Red()
# 設(shè)置加粗
textRange.Format.IsBold = TriState.TTrue
# 設(shè)置形狀邊框
shape.ShapeStyle.LineColor.Color = Color.get_Black()
ppt.SaveToFile("FormattedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
生成的 PowerPoint 演示文稿如下:

這里通過 TextFrame.Paragraphs[0] 訪問第一個段落,通過 TextRanges[0] 訪問第一個文本范圍,然后設(shè)置字體屬性。
多段落文本框
文本框可以包含多個段落,每個段落可以有不同的格式:
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
# 設(shè)置第一個段落
para0 = tf.Paragraphs[0]
para0.Text = "第一段:標(biāo)題文本"
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 = "第二段:正文內(nèi)容"
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 = "第三段:補(bǔ)充說明"
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()
生成的 PowerPoint 演示文稿如下:

通過 TextParagraph() 創(chuàng)建新段落,然后使用 tf.Paragraphs.Append() 方法添加到文本框中。
設(shè)置文本框邊距
文本框的內(nèi)邊距控制文本與邊框之間的距離:
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)
)
# 設(shè)置文本內(nèi)容
shape.TextFrame.Text = "設(shè)置了內(nèi)邊距的文本框,文本與邊框之間有合適的間距。"
# 設(shè)置文本框內(nèi)邊距
shape.TextFrame.MarginTop = 15 # 上邊距
shape.TextFrame.MarginBottom = 20 # 下邊距
shape.TextFrame.MarginLeft = 25 # 左邊距
shape.TextFrame.MarginRight = 25 # 右邊距
# 設(shè)置段落對齊方式
shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left
# 設(shè)置形狀樣式
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()
生成的 PowerPoint 演示文稿如下:

邊距的單位是磅(point),適當(dāng)設(shè)置邊距可以讓文本框內(nèi)容更加美觀。
段落對齊和縮進(jìn)
文本框中的段落可以設(shè)置對齊方式和縮進(jìn):
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
# 第一段:左對齊,無縮進(jìn)
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
# 第四段:左對齊,帶縮進(jìn)
para3 = TextParagraph()
tf.Paragraphs.Append(para3)
para3.Text = "縮進(jìn)文本示例"
para3.Alignment = TextAlignmentType.Left
para3.Indent = 50 # 縮進(jìn)50磅
ppt.SaveToFile("TextBoxAlignment.pptx", FileFormat.Pptx2013)
ppt.Dispose()
生成的 PowerPoint 演示文稿如下:

TextAlignmentType 枚舉提供了多種對齊方式,包括 Left、Center、Right、Justify 等。
實(shí)用技巧
設(shè)置行間距
shape.TextFrame.Paragraphs[0].LineSpacing = 150 # 1.5倍行距
行間距以百分比表示,100 表示單倍行距,150 表示 1.5 倍行距,200 表示雙倍行距。
設(shè)置文本方向
shape.TextFrame.Paragraphs[0].TextDirection = TextDirectionType.Vertical
文本可以設(shè)置為水平或垂直方向,適用于特殊排版需求。
移除文本框
如果需要從幻燈片中移除文本框:
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.SaveToFile("RemovedTextBox.pptx", FileFormat.Pptx2013)
ppt.Dispose()
總結(jié)
本文介紹了使用 Python 在 PowerPoint 中添加和設(shè)置文本框的基本方法,包括創(chuàng)建文本框、設(shè)置文本格式、添加多段落內(nèi)容、設(shè)置邊距和對齊方式等操作。通過這些技術(shù),可以實(shí)現(xiàn)演示文稿的自動化生成,提高工作效率。在實(shí)際應(yīng)用中,可以根據(jù)需要組合使用這些功能,創(chuàng)建符合特定需求的演示文稿模板。
以上就是Python操作PowerPoint實(shí)現(xiàn)添加與設(shè)置文本框完整教程的詳細(xì)內(nèi)容,更多關(guān)于Python操作PowerPoint的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python結(jié)合Spire.Presentation實(shí)現(xiàn)PowerPoint轉(zhuǎn)換為PDF
- Python腳本自動生成PowerPoint演示文件圖表
- Python操作Office(Word/Excel/PowerPoint)文檔的功能庫使用詳解
- 使用Python將PowerPoint演示文稿導(dǎo)出為HTML文件的操作指南
- 使用Python在PowerPoint演示文稿中添加動畫
- Python實(shí)現(xiàn)將PowerPoint(PPT/PPTX)轉(zhuǎn)為HTML
- Python實(shí)現(xiàn)在PowerPoint中添加和定制形狀
相關(guān)文章
Python中用字符串調(diào)用函數(shù)或方法示例代碼
字符串作為python中常用的數(shù)據(jù)類型,掌握字符串的常用方法十分必要。下面這篇文章主要給大家介紹了關(guān)于Python中通過字符串調(diào)用函數(shù)或方法的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
詳解python使用Nginx和uWSGI來運(yùn)行Python應(yīng)用
這篇文章主要介紹了詳解python使用Nginx和uWSGI來運(yùn)行Python應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
python庫lxml在linux和WIN系統(tǒng)下的安裝
這篇內(nèi)容我們給大家分享了lxml在WIN和LINUX系統(tǒng)下的簡單快速安裝過程,有興趣的朋友參考學(xué)習(xí)下。2018-06-06
Python實(shí)現(xiàn)合并兩個字典的8種方法
Python有多種方法可以通過使用各種函數(shù)和構(gòu)造函數(shù)來合并字典,本文主要介紹了Python實(shí)現(xiàn)合并兩個字典的8種方法,具有一定的參考價值,感興趣的可以了解一下2024-07-07
python中文件導(dǎo)入的使用(在同一目錄下和在不同目錄下)
在Python中,使用pathlib模塊的Path類可以方便地導(dǎo)入不同目錄下的文件,本文就來介紹一下,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
python http服務(wù)flask架構(gòu)實(shí)用代碼詳解分析
本篇文章主要分享一個python的簡單http服務(wù)flask架構(gòu)。目前主流的python的服務(wù)框架有django、flask,相較于django來說,flask更小巧玲瓏。至于并發(fā)的問題,使用了gevent協(xié)程io進(jìn)行處理2021-10-10
python自動打開瀏覽器下載zip并提取內(nèi)容寫入excel
這篇文章主要給大家介紹了關(guān)于python自動打開瀏覽器下載zip并提取內(nèi)容寫入excel的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python中的內(nèi)存管理之python list內(nèi)存使用詳解
這篇文章主要介紹了Python中的內(nèi)存管理之python list內(nèi)存使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

