在 Python 中向 PowerPoint PPT 幻燈片添加評論

通常,演示文稿中的內容需要由其他人審閱以獲得他們的反饋。在 PowerPoint 中,批註用於編寫有關特定單詞、短語或幻燈片上任何內容的反饋。在本文中,我們將介紹如何在 Python 中以編程方式向 PowerPoint PPT 幻燈片添加評論。此外,您將學習如何閱讀現有評論和添加回复。

在 PowerPoint PPT 中添加註釋的 Python 庫

要添加和閱讀評論及其回复,我們將使用 Aspose.Slides for Python via .NET。該庫旨在從頭開始創建豐富的 PowerPoint 演示文稿。此外,它允許您無縫地操作現有的演示文稿。您可以使用以下命令從 PyPI 在您的應用程序中安裝該庫。

> pip install aspose.slides

用 Python 給 PPT 幻燈片添加評論

在 PowerPoint 演示文稿中,評論與作者相關聯。然而,每條評論都包含一些附加信息,例如創建時間、添加幻燈片的位置及其位置。下面是使用 Python 在 PPT 中給幻燈片添加註釋的步驟。

  • 首先,加載演示文稿文件或使用 Presentation 類創建一個新文件。
  • 然後,添加新幻燈片或從 Presentation.slides 集合中獲取現有幻燈片的引用。
  • 使用 Presentation.commentauthors.addauthor(string, string) 方法將新作者添加到 authors 集合。
  • 獲取對像中新創建作者的引用。
  • 定義註釋的位置。
  • 使用 Author.comments.addcomment(string, ISlide, point, date) 方法添加評論。
  • 最後,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

下面的代碼示例展示瞭如何在 Python 中為 PPT 幻燈片添加評論。

import aspose.slides as slides
import aspose.pydrawing as drawing
import datetime

# 創建演示文稿
with slides.Presentation() as presentation:
    # 添加空白幻燈片
    presentation.slides.add_empty_slide(presentation.layout_slides[0])

    # 添加作者
    author = presentation.comment_authors.add_author("Usman", "MF")

    # 設置評論位置
    point = drawing.PointF(0.2, 0.2)

    # 在幻燈片 1 上為作者添加幻燈片評論
    author.comments.add_comment("Hello, this is slide comment", presentation.slides[0], point, datetime.date.today())

    # 在幻燈片 1 上為作者添加幻燈片評論
    author.comments.add_comment("Hello, this is second slide comment", presentation.slides[1], point, datetime.date.today())

    # 保存演示文稿
    presentation.save("ppt-comments.pptx", slides.export.SaveFormat.PPTX)

下面是我們添加評論後得到的幻燈片截圖。

在 Python 中向 PPT 幻燈片插入評論

用Python在PPT幻燈片中添加評論回复

Aspose.Slides 還允許您添加對評論的回复。回複本身是與現有評論相關聯的評論。那麼讓我們看看如何用Python在PowerPoint PPT幻燈片中添加評論回复。

  • 首先,加載演示文稿文件或使用 Presentation 類創建一個新文件。
  • 然後,添加新幻燈片或從 Presentation.slides 集合中獲取對現有幻燈片的引用。
  • 添加新作者並在對像中獲取其引用。
  • 使用 Author.comments.addcomment(string, ISlide, point, date) 方法插入評論並獲取返回的對象。
  • 以同樣的方式插入另一個評論並在對像中獲取其引用。
  • 使用 parentcomment 屬性設置第二條評論的父級。
  • 最後,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

以下代碼示例顯示瞭如何在 Python 中的 PPTX 演示文稿中添加對評論的回复。

import aspose.slides as slides
import aspose.pydrawing as drawing
import datetime

# 創建或加載演示文稿
with slides.Presentation() as presentation:
    # 添加空白幻燈片
    presentation.slides.add_empty_slide(presentation.layout_slides[0])

    # 添加作者和評論
    author = presentation.comment_authors.add_author("Usman", "MF")
    comment = author.comments.add_comment("Hello, this is slide comment.", presentation.slides[0], drawing.PointF(0.2, 0.2), datetime.date.today())

    # 添加回複評論
    reply = author.comments.add_comment("This is the reply to the comment.", presentation.slides[0], drawing.PointF(0.2, 0.2), datetime.date.today())
    reply.parent_comment = comment

    # 添加回複評論
    reply2 = author.comments.add_comment("This is second reply.", presentation.slides[0], drawing.PointF(0.2, 0.2), datetime.date.today())
    reply2.parent_comment = comment

    # 保存演示文稿
    presentation.save("ppt-comments.pptx", slides.export.SaveFormat.PPTX)

以下屏幕截圖顯示了上述代碼示例的輸出。

在PPT中添加對評論的回复

在 Python 中閱讀 PPT 幻燈片中的評論

您還可以閱讀特定作者或所有作者添加的 PPT 幻燈片中的評論。下面是用Python讀取PPT幻燈片評論的步驟。

  • 使用 Presentation 類加載演示文稿文件。
  • 使用 Presentation.commentauthors 集合遍歷作者列表。
  • 對於每個作者,使用 Author.comments 屬性循環瀏覽其評論。
  • 使用其屬性閱讀評論。

下面的代碼示例展示瞭如何在 Python 中讀取 PPT 幻燈片中的評論。

import aspose.slides as slides
import aspose.pydrawing as drawing
import datetime

with slides.Presentation("ppt-comments.pptx") as presentation:
    # 遍歷作者
   for author in presentation.comment_authors:
        # 循環評論
       for comment in author.comments:
            print("ISlide :" + str(comment.slide.slide_number) + 
            " has comment: " + comment.text + 
            " with Author: " + comment.author.name + 
            " posted on time :" + str(comment.created_time) + "\n")

獲得免費許可證

您可以通過 .NET 使用 Aspose.Slides for Python 而沒有評估限制,方法是申請 臨時許可證

結論

在本文中,您學習瞭如何使用 Python 在 PowerPoint PPT 幻燈片中添加評論。此外,我們還介紹瞭如何以編程方式在 PowerPoint 演示文稿中添加回復和閱讀評論。此外,您可以訪問 文檔 以通過 .NET 探索更多關於 Aspose.Slides for Python 的信息。此外,您可以將您的問題發佈到我們的論壇

也可以看看