Stories

Detail Return Return

用Python代碼實現PPT演示文稿到圖片的轉換 - Stories Detail

PowerPoint演示文稿作為展示創意、分享知識和表達觀點的重要工具,被廣泛應用於教育、商務彙報及個人項目展示等領域。然而,面對不同的分享場景與接收者需求,有時需要我們將PPT內容以圖片形式保存與傳播。這樣能夠避免軟件兼容性的限制,確保信息接收者無需安裝特定軟件即可查看內容,還便於在網絡社交平台、博客、電子郵件中快速分享與嵌入。而用Python代碼可以高效地實現PowerPoint演示文稿到圖片的批量轉換,從而提升工作效率。
文本將介紹如何使用Python實現PowerPoint演示文稿到圖片的轉換

  • 將PowerPoint幻燈片轉換為PNG圖片
  • 將PowerPoint幻燈片轉換為圖片並指定圖片大小
  • 將PowerPoint幻燈片轉換為SVG圖形文件

本文所使用的方法需要Spire.Presentation for Python,PyPI:pip install Spire.Presentation

申請免費License

將PowerPoint幻燈片轉換為JPG、PNG等圖片

我們可以使用庫中的Presentation.Slides[]屬性獲取指定的幻燈片,然後使用ISlide.SaveAsImage()方法將幻燈片保存為圖片流,之後再保存到圖片文件即可。
以下是詳細操作步驟:

  1. 導入所需模塊。
  2. 創建Presentation實例。
  3. 使用Presentation.LoadFromFile()方法從文件載入PowerPoint演示文稿。
  4. 遍歷演示文稿中的幻燈片:

    • 使用Presentation.Slides[]屬性獲取幻燈片。
    • 使用ISlide.SaveAsImage()方法將幻燈片保存為圖片流。
    • 使用Stream.Save()方法將圖片保存到文件。
  5. 釋放資源。

代碼示例

from spire.presentation import *
from spire.presentation.common import *

# 創建一個 Presentation 實例
presentation = Presentation()

# 加載一個演示文稿文件
presentation.LoadFromFile("示例.pptx")

# 遍歷演示文稿中的幻燈片
for i in range(presentation.Slides.Count):
    # 獲取當前幻燈片
    slide = presentation.Slides[i]
    # 將幻燈片保存為圖像流
    image = slide.SaveAsImage()
    # 將圖像保存到文件
    image.Save("output/PresentationToImage/Slide-" + str(i) + ".png")

# 釋放資源
presentation.Dispose()

轉換結果
Python保存PowerPoint幻燈片為圖片

將PowerPoint幻燈片轉換為圖片並指定圖片大小

庫中還提供了ISlide.SaveAsImageByWH()方法,以指定的寬度和高度,將幻燈片保存為圖片流。
以下是詳細操作步驟:

  1. 導入所需模塊。
  2. 創建Presentation實例。
  3. 使用Presentation.LoadFromFile()方法從文件載入PowerPoint演示文稿。
  4. 遍歷演示文稿中的幻燈片:

    • 使用Presentation.Slides[]屬性獲取幻燈片。
    • 使用ISlide.SaveAsSvg()方法將幻燈片保存指定高度和寬度的圖片流。
    • 使用Stream.Save()方法將圖片保存到文件。
  5. 釋放資源。

代碼示例:

from spire.presentation import *
from spire.presentation.common import *

# 創建一個 Presentation 實例
presentation = Presentation()

# 加載一個演示文稿文件
presentation.LoadFromFile("示例.pptx")

# 遍歷所有幻燈片
for i in range(presentation.Slides.Count):
    # 獲取幻燈片
    slide = presentation.Slides[i]
    # 將幻燈片保存為指定大小的圖像流
    image = slide.SaveAsImageByWH(800, 600)
    # 將圖像保存到文件
    image.Save("output/PresentationToImageWithSize/Slide" + str(i) + ".png")

# 釋放資源
presentation.Dispose()

轉換結果
Python將PowerPoint幻燈片轉換為指定寬度和高度的圖片

將PowerPoint幻燈片轉換為SVG圖形文件

除了轉換為普通的圖片外,該庫還提供一個ISlide.SaveToSvg()方法將幻燈片轉換為SVG格式的圖形。在轉換之前,還以通過Presentation.IsNoteRetained屬性設置是否在轉換時保留幻燈片中的備註。
以下是操作步驟:

  1. 導入所需模塊。
  2. 創建Presentation實例。
  3. 使用Presentation.LoadFromFile()方法從文件載入PowerPoint演示文稿。
  4. 通過Presentation.IsNoteRetained屬性設置是否在轉換時保留幻燈片中的備註。
  5. 遍歷演示文稿中的幻燈片:

    • 使用Presentation.Slides[]屬性獲取幻燈片。
    • 使用ISlide.SaveToSvg()方法將幻燈片保存為SVG圖形流。
    • 使用Stream.Save()方法將SVG圖形保存到文件。
  6. 釋放資源。

代碼示例

from spire.presentation.common import *
from spire.presentation import *

# 創建一個 Presentation 實例
presentation = Presentation()

# 加載一個演示文稿文件
presentation.LoadFromFile("示例.pptx")

# 設置是否保留備註
presentation.IsNoteRetained = False

# 遍歷幻燈片
for i in range(presentation.Slides.Count):
    # 獲取幻燈片
    slide = presentation.Slides[i]
    # 將幻燈片保存為 SVG 流
    svg = slide.SaveToSVG()
    # 將 SVG 流保存到文件
    svg.Save("output/PresentationToSvg/Slide-" + str(i) + ".svg")

# 釋放資源
presentation.Dispose()

轉換結果
Python轉換PowerPoint幻燈片為SVG圖形

本文介紹瞭如何使用Python代碼將PowerPoint演示文稿中的幻燈片保存到圖片及SVG圖形文件。

更多PowerPoint演示文稿處理技巧請前往Spire.Presentation for Python教程查看。

user avatar user_2dx56kla Avatar
Favorites 1 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.