在 Python via Java 中管理演示文稿缩放
介绍
PowerPoint 缩放功能可让您在幻灯片放映期间在特定幻灯片和章节之间导航。使用 Aspose.Slides for Python via Java,您可以创建幻灯片、章节和摘要缩放,并自定义其预览、边框和过渡效果。

幻灯片缩放
幻灯片缩放可以让您的演示更具动态性,允许您以任意顺序自由在幻灯片之间导航,而不会中断演示的流程。幻灯片缩放非常适合章节不多的简短演示,但在不同的演示场景中仍然可以使用。
幻灯片缩放让您可以从单个概览幻灯片探查相关信息。

对于幻灯片缩放对象,Aspose.Slides 提供了ZoomImageType枚举、ZoomFrame类以及ShapeCollection类中的方法。
创建缩放帧
使用ShapeCollection.addZoomFrame在概览幻灯片上放置指向单个幻灯片的链接。此示例创建两个具有不同背景和标签的目标幻灯片,然后将每个幻灯片的预览添加到第一张幻灯片,并将演示保存为 PPTX。帧的位置和尺寸以点为单位指定。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
second_slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
third_slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
second_slide.getBackground().setType(BackgroundType.OwnBackground)
second_slide.getBackground().getFillFormat().setFillType(FillType.Solid)
second_slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
text_box = second_slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 500, 200)
text_box.getTextFrame().setText("Second Slide")
third_slide.getBackground().setType(BackgroundType.OwnBackground)
third_slide.getBackground().getFillFormat().setFillType(FillType.Solid)
third_slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(189, 183, 107))
text_box = third_slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 500, 200)
text_box.getTextFrame().setText("Third Slide")
overview_slide.getShapes().addZoomFrame(20, 20, 250, 200, second_slide)
overview_slide.getShapes().addZoomFrame(200, 250, 250, 200, third_slide)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
使用自定义图像创建缩放帧
向ShapeCollection.addZoomFrame传递自定义图像,以替代目标幻灯片的默认预览。此示例需要本地图像,使用Images.fromFile加载它,并通过ImageCollection.addImage将其添加到演示中。生成的帧显示该图像,同时仍链接到第二张幻灯片。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Images, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().setType(BackgroundType.OwnBackground)
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
text_box = slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 500, 200)
text_box.getTextFrame().setText("Second Slide")
image = Images.fromFile("image.png")
try:
preview_image = presentation.getImages().addImage(image)
finally:
image.dispose()
overview_slide.getShapes().addZoomFrame(20, 20, 300, 200, slide, preview_image)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
格式化缩放帧
此示例自定义了两个幻灯片缩放帧。它通过ZoomObject.setZoomImage为第一个帧分配本地图像,并通过Shape.getLineFormat为第二个帧设置五点、虚线点划的粉红色边框。向ZoomObject.setShowBackground传递False可隐藏第二个帧的背景。自定义预览需要本地图像;两个帧在保存的 PPTX 中仍保留各自的幻灯片目标。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Images, LineDashStyle, Presentation, SaveFormat, ShapeType
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
second_slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
third_slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
second_slide.getBackground().setType(BackgroundType.OwnBackground)
second_slide.getBackground().getFillFormat().setFillType(FillType.Solid)
second_slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
text_box = second_slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 500, 200)
text_box.getTextFrame().setText("Second Slide")
third_slide.getBackground().setType(BackgroundType.OwnBackground)
third_slide.getBackground().getFillFormat().setFillType(FillType.Solid)
third_slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(189, 183, 107))
text_box = third_slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 500, 200)
text_box.getTextFrame().setText("Third Slide")
first_zoom_frame = overview_slide.getShapes().addZoomFrame(20, 20, 250, 200, second_slide)
second_zoom_frame = overview_slide.getShapes().addZoomFrame(200, 250, 250, 200, third_slide)
image = Images.fromFile("image.png")
try:
preview_image = presentation.getImages().addImage(image)
finally:
image.dispose()
first_zoom_frame.setZoomImage(preview_image)
second_zoom_frame.getLineFormat().setWidth(5)
second_zoom_frame.getLineFormat().getFillFormat().setFillType(FillType.Solid)
second_zoom_frame.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color(255, 105, 180))
second_zoom_frame.getLineFormat().setDashStyle(LineDashStyle.DashDot)
second_zoom_frame.setShowBackground(False)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
章节缩放
章节缩放链接到演示文稿中的一个章节。使用它可以重新访问某个主题或展示演示各部分之间的关联。

对于章节缩放对象,Aspose.Slides 提供了SectionZoomFrame类以及ShapeCollection类中的方法。
创建章节缩放帧
使用SectionCollection.addSection创建章节,然后将返回的章节传递给ShapeCollection.addSectionZoomFrame。此示例在第一张幻灯片上放置一个章节缩放,并将其链接到以彩色幻灯片开头的章节。帧的位置和尺寸以点为单位指定,演示保存为 PPTX。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(154, 205, 50))
slide.getBackground().setType(BackgroundType.OwnBackground)
section = presentation.getSections().addSection("Section 1", slide)
section_zoom_frame = overview_slide.getShapes().addSectionZoomFrame(20, 20, 300, 200, section)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
使用自定义图像创建章节缩放帧
ShapeCollection.addSectionZoomFrame的图像重载允许在不更改目标章节的情况下显示自定义预览。此示例需要本地图像,使用Images.fromFile加载,并通过ImageCollection.addImage添加。保存的演示在第一张幻灯片上包含一个链接到新创建章节的章节缩放。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Images, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(154, 205, 50))
slide.getBackground().setType(BackgroundType.OwnBackground)
image = Images.fromFile("image.png")
try:
preview_image = presentation.getImages().addImage(image)
finally:
image.dispose()
section = presentation.getSections().addSection("Section 1", slide)
section_zoom_frame = overview_slide.getShapes().addSectionZoomFrame(20, 20, 300, 200, section, preview_image)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
更改章节缩放的目标章节
使用SectionZoomFrame.setTargetSection可在不重新创建的情况下重新定向现有章节缩放。选择同一演示文稿的Presentation.getSections集合中的已填充章节。将空章节分配给目标将保持当前目标不变,故在分配前请检查Section.getSlidesListOfSection。有关章节的更多信息,请参阅管理幻灯片章节。
以下示例加载一个演示文件,该文件必须包含多个已填充章节、一个名为Section navigation的现有章节缩放以及一个唯一名称为Results的章节。它遍历所有幻灯片以查找缩放,并通过名称而非可能因章节重新排序而变化的集合索引来定位目标。如果名称不唯一,请使用Section.getSectionId来标识目标。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, SectionZoomFrame
presentation = Presentation("section-zoom.pptx")
try:
zoom = None
for slide in presentation.getSlides():
for shape in slide.getShapes():
if isinstance(shape, SectionZoomFrame) and shape.getName() == "Section navigation":
zoom = shape
break
if zoom is not None:
break
target_section = None
for section in presentation.getSections():
if section.getName() == "Results":
target_section = section
break
if zoom is None or target_section is None:
print("The section zoom or destination section was not found.")
elif target_section.getSlidesListOfSection().size() == 0:
print("The destination section is empty.")
else:
current_section = zoom.getTargetSection()
current_name = "" if current_section is None else current_section.getName()
print("Current section: " + str(current_name))
zoom.setTargetSection(target_section)
presentation.save("section-zoom-updated.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
格式化章节缩放帧
此示例将章节缩放移动并调整大小为 100 × 75 点,通过ZoomObject.setZoomImage分配本地图像,并应用 2.5 点、虚线点划的棕色边框。它启用ZoomObject.setReturnToParent以在章节结束后返回到来源幻灯片,禁用ZoomObject.setShowBackground,并将ZoomObject.setTransitionDuration设为 1.5 秒。自定义预览需要本地图像。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Images, LineDashStyle, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(154, 205, 50))
slide.getBackground().setType(BackgroundType.OwnBackground)
section = presentation.getSections().addSection("Section 1", slide)
section_zoom_frame = overview_slide.getShapes().addSectionZoomFrame(20, 20, 300, 200, section)
section_zoom_frame.setX(100)
section_zoom_frame.setY(300)
section_zoom_frame.setWidth(100)
section_zoom_frame.setHeight(75)
image = Images.fromFile("image.png")
try:
preview_image = presentation.getImages().addImage(image)
finally:
image.dispose()
section_zoom_frame.setZoomImage(preview_image)
section_zoom_frame.setReturnToParent(True)
section_zoom_frame.setShowBackground(False)
section_zoom_frame.getLineFormat().getFillFormat().setFillType(FillType.Solid)
section_zoom_frame.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color(165, 42, 42))
section_zoom_frame.getLineFormat().setDashStyle(LineDashStyle.DashDot)
section_zoom_frame.getLineFormat().setWidth(2.5)
section_zoom_frame.setTransitionDuration(1.5)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
摘要缩放
摘要缩放提供演示章节的概览。在幻灯片放映期间,您可以使用它跳转到章节、快速前进或重新访问主题,而无需遵循幻灯片顺序。

对于摘要缩放对象,Aspose.Slides 提供了SummaryZoomFrame、SummaryZoomSection和SummaryZoomSectionCollection类以及ShapeCollection类中的方法。
创建摘要缩放
使用ShapeCollection.addSummaryZoomFrame创建演示章节的概览。此示例创建四个具有不同幻灯片背景的章节,并在第一张幻灯片上添加摘要缩放。帧的位置和尺寸以点为单位指定,生成的概览保存为 PPTX 演示文稿。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(165, 42, 42))
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 1", slide)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 2", slide)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(127, 255, 0))
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 3", slide)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(0, 100, 0))
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 4", slide)
summary_zoom_frame = overview_slide.getShapes().addSummaryZoomFrame(150, 50, 300, 200)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
添加和移除摘要缩放章节
摘要缩放将其SummaryZoomSection对象存储在SummaryZoomFrame.getSummaryZoomCollection中。此示例创建两个章节的概览,使用SummaryZoomSectionCollection.addSummaryZoomSection添加第三个章节,并使用SummaryZoomSectionCollection.removeSummaryZoomSection移除第一章节的条目。移除条目后,演示章节及其幻灯片保持完整。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(165, 42, 42))
slide.getBackground().setType(BackgroundType.OwnBackground)
first_section = presentation.getSections().addSection("Section 1", slide)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 2", slide)
summary_zoom_frame = overview_slide.getShapes().addSummaryZoomFrame(150, 50, 300, 200)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(127, 255, 0))
slide.getBackground().setType(BackgroundType.OwnBackground)
third_section = presentation.getSections().addSection("Section 3", slide)
summary_zoom_frame.getSummaryZoomCollection().addSummaryZoomSection(third_section)
summary_zoom_frame.getSummaryZoomCollection().removeSummaryZoomSection(first_section)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
格式化摘要缩放章节
此示例格式化SummaryZoomFrame.getSummaryZoomCollection中的第一条目,使用基于零的索引0。它需要本地图像用于ZoomObject.setZoomImage,应用 1.5 点、虚线点划的黑色边框,并将ZoomObject.setTransitionDuration设为 1.5 秒。向ZoomObject.setReturnToParent传递False可禁用此条目返回摘要幻灯片的自动行为。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import BackgroundType, FillType, Images, LineDashStyle, Presentation, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
overview_slide = presentation.getSlides().get_Item(0)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color(165, 42, 42))
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 1", slide)
slide = presentation.getSlides().addEmptySlide(overview_slide.getLayoutSlide())
slide.getBackground().getFillFormat().setFillType(FillType.Solid)
slide.getBackground().getFillFormat().getSolidFillColor().setColor(Color.CYAN)
slide.getBackground().setType(BackgroundType.OwnBackground)
presentation.getSections().addSection("Section 2", slide)
summary_zoom_frame = overview_slide.getShapes().addSummaryZoomFrame(150, 50, 300, 200)
summary_section = summary_zoom_frame.getSummaryZoomCollection().get_Item(0)
image = Images.fromFile("image.png")
try:
preview_image = presentation.getImages().addImage(image)
finally:
image.dispose()
summary_section.setZoomImage(preview_image)
summary_section.setReturnToParent(False)
summary_section.getLineFormat().getFillFormat().setFillType(FillType.Solid)
summary_section.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
summary_section.getLineFormat().setDashStyle(LineDashStyle.DashDot)
summary_section.getLineFormat().setWidth(1.5)
summary_section.setTransitionDuration(1.5)
presentation.save("presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
常见问题
我可以控制在显示目标后返回“父”幻灯片吗?
可以。调用ZoomObject.setReturnToParent并传入True即可在目标内容显示后返回来源幻灯片。
我可以调整缩放过渡的“速度”或时长吗?
可以。使用ZoomObject.setTransitionDuration来控制过渡时长(秒)。
演示文稿中可以包含多少个缩放对象有上限吗?
文档中未列出硬性 API 限制。实际限制取决于演示的整体复杂度和查看器的性能。您可以添加大量缩放帧,但请考虑文件大小和渲染时间。