给 PowerPoint 文档中应用母版页能够为演示文稿提供一致的设计和格式,使得整体外观更加专业。通过母版可以批量修改字体、颜色和布局,减少手动调整每张幻灯片的繁琐工作,提高效率。在本文中,我们将演示如何使用 Spire.Presentation for Python 在 Python 中操作 PowerPoint 文档里母版页,包括创建并应用母版页、移除未使用母版页、克隆母版页。
安装 Spire.Presentation for Python
本教程需要用到 Spire.Presentation for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Presentation
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.Presentation for Python
在 PowerPoint 文档中创建并应用母版页
Spire.Presentation for Python 提供了 Presentation.Masters.AppendSlide(IMasterSlide slide) 方法用于给 PowerPoint 文档新增母版页;提供了 IMasterSlide.Slides[Index].Layout 来获取指定的母版页的页面布局。再通过 Presentation.Slide[Index].Layout 属性将获取的母版页页面布局设置给指定幻灯片,具体步骤如下:
- 创建 Presentation 类的对象。
- 使用 Presentation.SlideSize.Type 属性设置当前 PowerPoint 演示文稿的尺寸。
- 通过 Presentation.Slides.Append() 方法给当前 PowerPoint 演示文稿添加4张新的幻灯片,加上初始化 Presentation 时默认会新增一张幻灯片,此时文档中共有5张幻灯片。
- 通过 Presentation.Masters[Index] 来获取第一个默认幻灯片母版。
- 通过 Presentation.Masters.AppendSlide(IMasterSlide slide) 方法来新增一个幻灯片母版。
- 通过 IMasterSlide.SlideBackground.Fill.FillType 来设置幻灯片母版填充类型为图片。
- 通过 IMasterSlide.Shapes.AppendEmbedImageByPath(ShapeType shapeType, IImageData embedImage, RectangleF rectangle) 来把图片读取到第一个幻灯片母版页中。
- 通过 IMasterSlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage 来设置第一个幻灯片母版的背景图片。
- 重复前面2个步骤为第二个幻灯片母版设置背景图片。
- 通过 IMasterSlide.Slides[Index].Layout 获取母版页页面布局,并通过 Presentation.Slide[Index].Layout 给第一张幻灯片的页面布局设置为第一张幻灯片母版页的页面布局,给第二到五张幻灯片的页面布局设置为第二张幻灯片母版页的页面布局。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 设置输出文件路径
outputFile = "output.pptx"
# 创建一个新的PPT演示文稿对象
ppt = Presentation()
# 设置幻灯片尺寸为16:9的屏幕尺寸
ppt.SlideSize.Type = SlideSizeType.Screen16x9
# 添加4个空白幻灯片到演示文稿中
for i in range(0, 4):
ppt.Slides.Append()
# 获取第一个幻灯片母版
first_master = ppt.Masters[0]
# 为PowerPoint添加第二个母版页
ppt.Masters.AppendSlide(first_master)
second_master = ppt.Masters[1]
# 设置两个图片的路径
pic1 = "image/bg.png"
pic2 = "image/Setbackground.png"
# 创建一个矩形区域,覆盖整个幻灯片尺寸
rect = RectangleF.FromLTRB (0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height)
# 设置第一个幻灯片母版的填充类型为图片
first_master.SlideBackground.Fill.FillType = FillFormatType.Picture
# 在第一个幻灯片母版中嵌入背景图片
image1 = first_master.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, pic1, rect)
# 设置第一个幻灯片母版的背景图片
first_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image1.PictureFill.Picture.EmbedImage
# 设置第二个幻灯片母版的填充类型为图片
second_master.SlideBackground.Fill.FillType = FillFormatType.Picture
# 在第二个幻灯片母版中嵌入背景图片
image2 = second_master.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, pic2, rect)
# 设置第二个幻灯片母版的背景图片
second_master.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image2.PictureFill.Picture.EmbedImage
# 将第一个幻灯片的布局设置为第一个幻灯片母版的第二个子布局
ppt.Slides[0].Layout = first_master.Layouts[1]
# 将剩余的幻灯片布局设置为第二个幻灯片母版的第九个子布局
for i in range(1, ppt.Slides.Count):
ppt.Slides[i].Layout = second_master.Layouts[8]
# 保存演示文稿到指定的文件路径,使用Pptx2013格式
ppt.SaveToFile(outputFile, FileFormat.Pptx2013)
# 释放演示文稿资源
ppt.Dispose()
在 PowerPoint 文档中移除未使用的母版页
对于一个已经制作完成的在 PowerPoint 文档,有时会存在一些没有被任何幻灯片使用的母版页,那么如何使用 Spire.Presentation for Python 来删除这些不必要的母版页呢?首先,我们需要把每张幻灯片使用到的页面布局 ID 存放进一个名为 layouts 的列表中,接着遍历 PowerPoint 文档的所有母版布局,如果当前母版布局的 ID 不在列表 layouts 中,则表明当前母版布局没有被使用,调用 IMasterLayouts. RemoveMasterLayout 删除当前母版页。具体步骤如下:
- 创建 Presentation 类的对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 PowerPoint 演示文稿。
- 遍历 PowerPoint 文档中所有幻灯片,通过 Presentation.Slides[Index].Layout.SlideID 获取当前幻灯片的布局ID,并将其添加到一个名为 layouts 的列表中。
- 遍历遍历 PowerPoint 文档中所有母版页,通过 Presentation.Master[Index].Layouts 获取到当前母版布局的所有子布局集合 IMasterLayouts,再遍历这个子布局集合,通过 IMasterLayouts[Index].SlideID 获取当前子布局的 ID,并判断该 ID 是否在 layouts 这个列表中,如果不在,表明当前母版布局没有被使用,通过 IMasterLayouts. RemoveMasterLayout 方法删除当前母版页。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 输入文件路径,指向一个PPTX格式的演示文稿
inputFile = "input.pptx"
# 输出文件路径,用于保存处理后的演示文稿
outputFile = "output.pptx"
# 创建一个Presentation对象,用于操作PowerPoint演示文稿
ppt = Presentation()
# 从指定路径加载演示文稿到Presentation对象中
ppt.LoadFromFile(inputFile)
# 创建一个空列表,用于存储所有幻灯片使用的布局ID
layouts = []
# 遍历演示文稿中的所有幻灯片
for i, unusedItem in enumerate(ppt.Slides):
# 获取当前幻灯片的布局对象
layout = ppt.Slides[i].Layout
# 将当前幻灯片的布局ID添加到layouts列表中
layouts.append(layout.SlideID)
# 遍历演示文稿中的所有母版布局
for i, unusedItem in enumerate(ppt.Masters):
# 获取当前母版布局的所有子布局集合
masterlayouts = ppt.Masters[i].Layouts
# 从后向前遍历子布局集合,以便在删除时不影响索引
for j in range(masterlayouts.Count - 1, -1, -1):
# 如果当前子布局的ID不在layouts列表中,说明该布局未被使用
if not masterlayouts[j].SlideID in layouts:
# 从母版布局中移除未使用的子布局
masterlayouts.RemoveMasterLayout(j)
# 将处理后的演示文稿保存到指定的输出文件路径,使用Pptx2013格式
ppt.SaveToFile(outputFile, FileFormat.Pptx2013)
# 释放Presentation对象的资源
ppt.Dispose()
克隆一个 PowerPoint 文档中的母版页到另一个 PowerPoint 文档中
有时我们需要在演示文稿B中使用演示文稿 A 中定义好的母版页,我们就可以将演示文稿 A 中的母版克隆到演示文稿 B 中,这样就能在演示文稿 B 中使用相应母版页了,具体步骤如下:
- 创建2个 Presentation 类的对象,presentation1 和 presentation2。
- 使用 Presentation.LoadFromFile() 方法来加载2个待操作的 PowerPoint 演示文稿。
- 遍历 presentation1 的 PowerPoint 文档中所有母版页,再通过 Presentation.Master.AppendSlide() 方法将 presentation1 中所有母版页添加到 presentation2 中。
- 使用 Presentation.SaveToFile() 方法保存 presentation2 结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation.common import *
from spire.presentation import *
# 定义输入文件路径1,指向第一个PPTX文件
inputFile_1 = "input/CloneMaster1.pptx"
# 定义输入文件路径2,指向第二个PPTX文件
inputFile_2 = "input/CloneMaster2.pptx"
# 定义输出文件路径,用于保存合并后的PPTX文件
outputFile ="output/ClonePPTMasterToAnother.pptx"
# 创建一个Presentation对象,用于操作第一个PPTX文件
presentation1 = Presentation()
# 从文件加载第一个PPTX文件的内容到presentation1对象中
presentation1.LoadFromFile(inputFile_1)
# 创建另一个Presentation对象,用于操作第二个PPTX文件
presentation2 = Presentation()
# 从文件加载第二个PPTX文件的内容到presentation2对象中
presentation2.LoadFromFile(inputFile_2)
# 遍历第一个PPTX文件中的所有母版幻灯片
for masterSlide in presentation1.Masters:
# 将每个母版幻灯片添加到第二个PPTX文件的母版幻灯片集合中
presentation2.Masters.AppendSlide(masterSlide)
# 将合并后的第二个PPTX文件内容保存到指定的输出文件中,使用Pptx2013格式
presentation2.SaveToFile(outputFile, FileFormat.Pptx2013)
# 释放二个PPTX文件对象的资源
presentation2.Dispose()
presentation1.Dispose()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。