Python實現PowerPoint(PPT/PPTX)到PDF的批量轉換

如果需要處理大量的PPT轉PDF的工作,一個個打開並另存爲PDF是非常費時的做法。我們可以利用Python編程語言的強大的工具來自動化這個過程,使得批量轉換變得簡單而高效。本文將介紹如何使用Python將PowerPoint演示文稿(PPT、PPTX等)轉換爲PDF文件,使演示內容能夠在更多的設備上展示,且保持內容展示效果一致。同時給大家分享一款免費的在線轉換工具,將各種格式文件轉換爲PDF文件。

本文所使用的方法需要用到Spire.Presentation for Python,可從官網下載或通過PyPI安裝:

pip install Spire.Presentation

 

將PowerPoint演示文稿批量轉換爲PDF文件

加載PPT文件後,調用 Presentation.SaveToFile(fileName: str, fileFormat: FileFormat) 方法可將PPT轉換爲 PDF 文檔。以下是執行此操作的詳細步驟:

  • 創建一個 Presentation 類對象。
  • 使用 Presentation.LoadFromFile() 方法加載演示文稿文件。
  • 使用 Presentation.SaveToFile(fileName: str, fileFormat: FileFormat) 方法將演示文稿保存爲 PDF 文檔。

Python代碼示例:

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

# 設置文件夾路徑和輸出文件夾路徑
folder_path = "Documents/"
output_folder = "Output/"

# 遍歷文件夾中的文件
for file_name in os.listdir(folder_path):
    file_path = os.path.join(folder_path, file_name)
    
    # 判斷文件名是否以.pptx或.ppt結尾
    if file_name.lower().endswith('.pptx') or file_name.lower().endswith('.ppt'):
        
        # 根據文件名生成輸出路徑
        output_path = os.path.join(output_folder, os.path.splitext(file_name)[0] + '.pdf')
        
        # 創建Presentation對象並從文件加載演示文稿
        presentation = Presentation()
        presentation.LoadFromFile(file_path)
        
        # 將演示文稿保存爲PDF格式到指定輸出文件夾
        presentation.SaveToFile(output_folder, FileFormat.PDF)
        
        # 釋放Presentation對象佔用的資源
        presentation.Dispose()

 

將PPT轉換爲PDF文件並設置頁面大小

在轉換過程中,還可以使用 Presentation.SlideSize.Type 屬性設置幻燈片大小,從而決定生成的 PDF 文檔的頁面大小。以下是此操作的詳細步驟:

  • 創建一個 Presentation 類對象。
  • 使用 Presentation.LoadFromFile() 方法加載演示文稿文件。
  • 使用 Presentation.SlideSize.Type 屬性將幻燈片尺寸設置爲 A4。
  • 使用 Presentation.SaveToFile(file: str, fileFormat: FileFormat) 方法將演示文稿保存爲 PDF 文檔。

Python代碼示例:

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

# 導入所需的模塊

# 創建Presentation類的對象
presentation = Presentation()

# 從文件加載演示文稿
presentation.LoadFromFile("Sample.pptx")

# 將幻燈片大小更改爲A4
presentation.SlideSize.Type = SlideSizeType.A4

# 將演示文稿轉換爲PDF並保存
presentation.SaveToFile("output/PresentationToPDFA4.pdf", FileFormat.PDF)
presentation.Dispose()

 

將某張幻燈片轉換爲PDF

我們還可以將PPT文件中的單張幻燈片轉換爲 PDF 文檔。具體操作步驟如下:

  • 創建一個 Presentation 類對象。
  • 使用 Presentation.LoadFromFile() 方法加載演示文稿文件。
  • 使用 Presentation.Slides[] 屬性獲取幻燈片。
  • 使用 ISlde.SaveToFile(file: str, fileFormat: FileFormat) 方法將幻燈片保存爲 PDF 文檔。

Python代碼示例:

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

# 導入所需的模塊

# 創建Presentation類的對象
presentation = Presentation()

# 從文件加載演示文稿
presentation.LoadFromFile("Sample.pptx")

# 獲取一張幻燈片
slide = presentation.Slides[1]

# 將幻燈片保存爲PDF文件
slide.SaveToFile("output/SlideToPDF.pdf", FileFormat.PDF)
presentation.Dispose()

 

免費在線轉換工具

Free Spire.PDFConverter for .NET是一個免費的在線轉換工具,支持將各種格式的文件轉換爲PDF文件,包括演示文稿(PPT、PPTX等)。只需要選擇並上傳文件,等待上傳完成後點擊“Download”即可。

 

總結

本文介紹了使用Python將PowerPoint演示文稿(PPT、PPTX等格式文件)轉換爲PDF文件,如何在轉換時設置頁面大小,以及如何轉換單頁幻燈片爲PDF文件。同時,本文還提供了一個免費在線轉換工具,支持將各種文件轉換爲PDF文件並下載保存。Spire.PDF for Python還支持許多其他功能,請前往Spire.Presentation for Python教程查看。如果在使用中遇到問題,可前往Spire論壇獲取技術支持。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章