使用 Python 编辑 PowerPoint

作为 Python 开发人员,您可以轻松地以编程方式编辑 PowerPoint 演示文稿。您可以更新幻灯片内容,而无需使用 Python 安装任何外部应用程序。本文将重点介绍如何在 Python 中使用 REST API 编辑 PowerPoint 演示文稿。

本文应涵盖以下主题:

文档编辑器 REST API 和 Python SDK

为了编辑 PPTX,我将使用 GroupDocs.Editor Cloud 的 Python SDK API。它允许您以编程方式编辑字处理文档、Excel 工作表或其他支持格式的文档。它还为云 API 提供 .NET、Java、PHP、Ruby、Android 和 Node.js SDK 作为其文档编辑器家族成员

您可以在控制台中使用以下命令将 GroupDocs.Editor-Cloud 安装到您的 Python 项目:

pip install groupdocs_editor_cloud

在开始执行步骤和可用代码示例之前,请从 仪表板 获取您的客户端 ID 和客户端密码。获得 ID 和密码后,添加如下所示的代码:

client_id = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5"
client_secret = "479db2b01dcb93a3d4d20efb16dea971"

configuration = groupdocs_editor_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

在 Python 中使用 REST API 编辑 PowerPoint 演示文稿

您可以按照以下简单步骤编辑 PowerPoint 演示文稿:

  1. 上传PPTX文件到云端
  2. 编辑上传的文件
  3. 下载更新后的文件

上传文件

首先,使用下面给出的代码示例将 PowerPoint 演示文稿上传到云端:

# 创建 api 实例
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# 上传示例文件
request = groupdocs_editor_cloud.UploadFileRequest("sample.pptx", "C:\\Files\\sample.pptx", my_storage)
response = file_api.upload_file(request)

结果,PPTX 文件将上传到 Cloud Storage,并且可以在仪表板的 文件部分 中找到。

使用 Python 编辑 PowerPoint 演示文稿

请按照下面提到的步骤以编程方式编辑 PowerPoint 演示文稿。

  • 创建 File APIEdit API 实例
  • 提供输入文件路径
  • 提供 PresentationLoadOptions
  • 使用 Edit API 的 Load 方法加载文件
  • 使用 File API 的 Download File 方法下载 HTML 文档
  • 编辑下载的 HTML 文档
  • 使用 File API 的 Upload File 方法上传 HTML
  • 提供 PresentationSaveOptions 以保存在 PPTX 中
  • 使用 Edit API 的 Save 方法将 HTML 保存回 PPTX

以下代码片段显示了如何使用 REST API 更新 PowerPoint 演示文稿文档。

# 接口初始化
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)

# 将其加载到可编辑状态
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadOptions.show_hidden_slides = True
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions)) 

# 下载html文件
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""     

# 读取 HTML 文件
with open(htmlFile, 'r') as file:
    html = file.read() 

# 替换文字    
html = html.replace("Hello World", "Welcome")

# 将 HTML 上传回存储
with open(htmlFile, 'w') as file:
    file.write(html)

fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))

# 将 HTML 保存回 PPTX
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveOptions.password = "password"
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))

# 完毕
print("Document edited: " + saveResult.path)
使用 Python 编辑 PowerPoint 演示文稿

使用 Python 编辑 PowerPoint 演示文稿

下载更新文件

以上代码示例将编辑后的 PowerPoint 演示文稿 (PPTX) 文件保存在云端。您可以使用以下代码示例下载它:

# 接口初始化
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# 下载文件
request = groupdocs_editor_cloud.DownloadFileRequest("edited.pptx", my_storage)
response = file_api.download_file(request)

# 将下载的文件移动到您的工作目录
shutil.move(response, "C:\\Files\\")

使用 Python 更新 PowerPoint 演示文稿中的图像

请按照下面提到的步骤以编程方式更新 PowerPoint 演示文稿中的图像。

  • 创建 File APIEdit API 实例
  • 提供输入文件路径
  • 提供 PresentationLoadOptions
  • 使用 Edit API 的 Load 方法加载文件
  • 使用 File API 的 Download File 方法下载 HTML 文档
  • 上传图像文件
  • 编辑下载的 HTML 文档 and update the image
  • 使用 File API 的 Upload File 方法上传 HTML
  • 提供 PresentationSaveOptions 以保存在 PPTX 中
  • 使用 Edit API 的 Save 方法将 HTML 保存回 PPTX

以下代码片段显示了如何使用 REST API 更新 PowerPoint 演示文稿幻灯片上的图像。

# 接口初始化
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)

# 将其加载到可编辑状态
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions)) 

# 下载html文件
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""     

# 读取 HTML 文件
with open(htmlFile, 'r') as file:
    html = file.read() 

# 上传图片替换
request = groupdocs_editor_cloud.UploadFileRequest(loadOptions.output_path + "/sample.files/groupdocs.png", "C:\\Files\\groupdocs.png", "")
response = fileApi.upload_file(request)

# 更换图片   
html = html.replace("Picture 2.png", "groupdocs.png")

# 将 HTML 上传回存储
with open(htmlFile, 'w') as file:
    file.write(html)

fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))

# 将 HTML 保存回 PPTX
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))
更新 PowerPoint 演示文稿幻灯片中的图像

更新 PowerPoint 演示文稿幻灯片中的图像

API 在定义的 PresentationLoadOptions.output\path 中创建一个 HTML 文件。与创建的 HTML 文件关联的所有资源文件都放置在文件子目录中,该子目录以输入文件名(如本例中的“sample.files”)为前缀。您需要上传该目录下的图片,然后将其替换为目标图片。幻灯片上的所有图像在“src”属性中被命名为图片 2、图片 3 等。

在线试用

请试用以下使用上述API开发的免费在线PowerPoint编辑工具。 https://products.groupdocs.app/editor/pptx

结论

在本文中,您了解了如何使用 Python 使用 Document Editor REST API 在云端编辑 PowerPoint 演示文稿。您还学习了如何以编程方式将 PPTX 文件上传到云端,然后从云端下载更新后的文件。您可以使用 文档 了解有关 GroupDocs.Editor Cloud API 的更多信息。我们还提供了一个 API 参考 部分,让您可以直接通过浏览器可视化我们的 API 并与之交互。如有任何歧义,请随时在论坛上与我们联系。

也可以看看