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论坛获取技术支持。



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