使用 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 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。

也可以看看