Quản lý Zoom cho bài thuyết trình trong Python qua Java

Giới thiệu

Thu phóng PowerPoint cho phép bạn di chuyển giữa các slide và phần cụ thể trong khi trình chiếu. Với Aspose.Slides cho Python thông qua Java, bạn có thể tạo các zoom slide, phần và tổng hợp và tùy chỉnh bản xem trước, viền và chuyển đổi của chúng.

Tổng quan thu phóng PowerPoint

  • Để tóm tắt toàn bộ bài thuyết trình trên một slide duy nhất, sử dụng Zoom tổng hợp.
  • Để hiển thị chỉ các slide đã chọn, sử dụng Zoom slide.
  • Để hiển thị chỉ một phần duy nhất, sử dụng Zoom phần.

Zoom slide

Một zoom slide có thể làm cho bài thuyết trình của bạn trở nên năng động hơn, cho phép bạn di chuyển tự do giữa các slide theo bất kỳ thứ tự nào bạn chọn mà không làm gián đoạn luồng trình chiếu. Zoom slide rất hữu ích cho các bài thuyết trình ngắn không có nhiều phần, nhưng bạn vẫn có thể sử dụng chúng trong các kịch bản thuyết trình khác nhau.

Zoom slide cho phép bạn khám phá thông tin liên quan từ một slide tổng quan duy nhất.

Chọn các slide cho một zoom slide

Đối với các đối tượng zoom slide, Aspose.Slides cung cấp enumeration ZoomImageType, lớp ZoomFrame và các phương thức trên lớp ShapeCollection.

Tạo khung Zoom

Sử dụng ShapeCollection.addZoomFrame để đặt liên kết tới các slide riêng lẻ trên một slide tổng quan. Ví dụ này tạo hai slide đích với nền và nhãn khác nhau, sau đó thêm bản xem trước của mỗi slide vào slide đầu tiên và lưu bài thuyết trình dưới dạng PPTX. Vị trí và kích thước khung được chỉ định bằng điểm.

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()

Tạo khung Zoom với hình ảnh tùy chỉnh

Truyền một hình ảnh tùy chỉnh vào ShapeCollection.addZoomFrame để thay thế bản xem trước mặc định của slide đích. Ví dụ này yêu cầu một ảnh cục bộ, tải nó bằng Images.fromFile, và thêm nó vào bài thuyết trình bằng ImageCollection.addImage. Khung kết quả hiển thị hình ảnh đó trong khi vẫn liên kết tới slide thứ hai.

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()

Định dạng khung Zoom

Ví dụ này tùy chỉnh hai khung zoom slide. Nó gán một hình ảnh cục bộ cho khung đầu tiên thông qua ZoomObject.setZoomImage, và cho khung thứ hai một viền hồng dash‑dot rộng năm điểm thông qua Shape.getLineFormat. Truyền False vào ZoomObject.setShowBackground sẽ ẩn nền của khung thứ hai. Một hình ảnh cục bộ là bắt buộc cho bản xem trước tùy chỉnh; cả hai khung vẫn giữ mục tiêu slide trong file PPTX đã lưu.

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()

Zoom phần

Zoom phần liên kết tới một phần trong bài thuyết trình của bạn. Sử dụng nó để quay lại một chủ đề hoặc hiển thị cách các phần khác nhau của bài thuyết trình kết nối với nhau.

Chọn một phần cho zoom phần

Đối với các đối tượng zoom phần, Aspose.Slides cung cấp lớp SectionZoomFrame và các phương thức trên lớp ShapeCollection.

Tạo khung Zoom phần

Tạo một phần bằng SectionCollection.addSection, sau đó truyền phần vừa tạo vào ShapeCollection.addSectionZoomFrame. Ví dụ này đặt một zoom phần trên slide đầu tiên và liên kết nó tới một phần bắt đầu bằng một slide có màu. Vị trí và kích thước khung được chỉ định bằng điểm, và bài thuyết trình được lưu dưới dạng 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()

Tạo khung Zoom phần với hình ảnh tùy chỉnh

Phiên bản chấp nhận hình ảnh của ShapeCollection.addSectionZoomFrame cho phép bạn hiển thị một bản xem trước tùy chỉnh mà không thay đổi phần đích. Ví dụ này yêu cầu một ảnh cục bộ, được tải bằng Images.fromFile và thêm qua ImageCollection.addImage. Bài thuyết trình đã lưu chứa một zoom phần trên slide đầu tiên, liên kết tới phần mới tạo.

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()

Thay đổi phần mục tiêu của Zoom phần

Sử dụng SectionZoomFrame.setTargetSection để chuyển hướng một zoom phần hiện có mà không cần tạo lại. Chọn một phần đã được điền từ bộ sưu tập Presentation.getSections của cùng một bài thuyết trình. Gán một phần rỗng sẽ giữ nguyên mục tiêu hiện tại, vì vậy hãy kiểm tra Section.getSlidesListOfSection trước khi gán. Xem Manage Slide Sections để biết thêm thông tin về các phần.

Ví dụ dưới đây tải một tệp bài thuyết trình, trong đó phải chứa nhiều phần đã được điền, một zoom phần hiện có tên Section navigation, và một phần có tên duy nhất Results. Nó tìm tất cả các slide cho zoom và xác định đích bằng tên thay vì bằng chỉ mục bộ sưu tập có thể thay đổi khi các phần được sắp xếp lại. Nếu tên không duy nhất, hãy sử dụng Section.getSectionId để xác định đích.

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()

Định dạng khung Zoom phần

Ví dụ này di chuyển và thay đổi kích thước một zoom phần thành 100×75 điểm, gán một hình ảnh cục bộ qua ZoomObject.setZoomImage, và áp dụng viền nâu dash‑dot rộng 2,5 điểm. Nó bật ZoomObject.setReturnToParent để trở lại slide gốc sau phần, tắt ZoomObject.setShowBackground, và đặt ZoomObject.setTransitionDuration thành 1,5 giây. Một hình ảnh cục bộ là bắt buộc cho bản xem trước tùy chỉnh.

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()

Zoom tổng hợp

Zoom tổng hợp cung cấp một cái nhìn tổng quan về các phần của bài thuyết trình. Trong khi trình chiếu, bạn có thể sử dụng nó để chuyển tới một phần, bỏ qua một đoạn, hoặc quay lại một chủ đề mà không phải tuân theo thứ tự slide.

Chọn các phần cho zoom tổng hợp

Đối với các đối tượng zoom tổng hợp, Aspose.Slides cung cấp các lớp và phương thức SummaryZoomFrame, SummaryZoomSection, và SummaryZoomSectionCollection trên lớp ShapeCollection.

Tạo một Zoom tổng hợp

Sử dụng ShapeCollection.addSummaryZoomFrame để tạo một cái nhìn tổng quan về các phần của bài thuyết trình. Ví dụ này tạo bốn phần với nền slide khác nhau và thêm một zoom tổng hợp vào slide đầu tiên. Vị trí và kích thước khung được chỉ định bằng điểm, và cái nhìn tổng quan được lưu trong một bài thuyết trình 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()

Thêm và xóa một phần Zoom tổng hợp

Một zoom tổng hợp lưu trữ các đối tượng SummaryZoomSection trong SummaryZoomFrame.getSummaryZoomCollection. Ví dụ này tạo một cái nhìn tổng quan của hai phần, thêm một phần thứ ba bằng SummaryZoomSectionCollection.addSummaryZoomSection, và xóa mục của phần đầu tiên bằng SummaryZoomSectionCollection.removeSummaryZoomSection. Việc xóa mục không ảnh hưởng đến phần và các slide của bài thuyết trình.

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()

Định dạng các phần Zoom tổng hợp

Ví dụ này định dạng mục đầu tiên trong SummaryZoomFrame.getSummaryZoomCollection, sử dụng chỉ mục bắt đầu từ 0. Nó yêu cầu một hình ảnh cục bộ cho ZoomObject.setZoomImage, áp dụng viền đen dash‑dot rộng 1,5 điểm, và đặt ZoomObject.setTransitionDuration thành 1,5 giây. Truyền False vào ZoomObject.setReturnToParent sẽ vô hiệu hoá việc tự động quay lại slide tổng hợp cho mục này.

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()

Câu hỏi thường gặp

Tôi có thể kiểm soát việc trở lại slide “cha” sau khi hiển thị mục tiêu không?

Có. Gọi ZoomObject.setReturnToParent với True để trở lại slide gốc sau khi nội dung mục tiêu được hiển thị.

Tôi có thể điều chỉnh “tốc độ” hoặc thời lượng của chuyển đổi Zoom không?

Có. Sử dụng ZoomObject.setTransitionDuration để kiểm soát thời lượng chuyển đổi tính bằng giây.

Có giới hạn về số lượng đối tượng Zoom mà một bài thuyết trình có thể chứa không?

Không có giới hạn API cứng được tài liệu hoá. Các giới hạn thực tế phụ thuộc vào độ phức tạp tổng thể của bài thuyết trình và hiệu năng của trình xem. Bạn có thể thêm nhiều khung Zoom, nhưng hãy cân nhắc kích thước tệp và thời gian render.