學習將 PPTX 轉換為 ODP 以及其他流行文件格式,包括 OpenDocument 格式的步驟。使用 Python SDK 將 mspowerpoint 文件保存為 ODP 格式。

Microsoft PowerPoint 轉 OpenDocument / 將 MS PowerPoint (PPTX) 轉換為 ODP 格式
PPTX 檔案是 Microsoft PowerPoint 應用程式的主要 PowerPoint 格式之一。而 OpenDocument Presentation (ODP) 也是一個流行的格式,支持 Apache OpenOffice,並且它包含一系列的幻燈片,這些幻燈片本身可能包含文本、圖像、媒體和過渡效果。因此,由於 OpenOffice 的流行,我們將學習如何使用 Python SDK 將 Microsoft PowerPoint (PPTX) 轉換為 OpenDocument 格式的步驟。
MS PowerPoint 處理 API
Aspose.Slides Cloud 能夠以程式化的方式創建、編輯和轉換 PowerPoint 簡報到其他支持的格式,包括 ODP。現在,為了在 Python 應用程序中實現相同的功能,可以使用 Aspose.Slides Cloud SDK for Python(Cloud API 的一個包裝器)。它可以從 PIP 和 GitHub 下載。請在命令行終端執行以下命令來安裝 SDK。
pip install asposeslidescloud
安裝完成後,您需要獲取個性化的 ClientID 和 ClientSecret 詳細資訊。因此,第一步是通過訪問 Aspose.Cloud dashboard 來創建一個帳戶。
將 Microsoft PowerPoint 轉換為 ODP 使用 Python
在下面的範例中,我們從雲端儲存中載入 Microsoft PowerPoint (PPTX 文件),然後將生成的 ODP 保存到相同的雲端儲存中。請按照以下指示執行轉換。
- 建立 Configuration 類別的物件
- 初始化 SlidesApi 實例,同時傳遞 Configuration 物件作為參數
- 現在呼叫方法 savepresentation(..),該方法接受輸入 PPTX 的名稱、輸出格式值為 ODP、結果文件的輸出路徑資訊以及一個可選參數,用於指定要轉換的幻燈片。
# 若要獲取更多範例,請訪問 https://github.com/aspose-slides-cloud/aspose-slides-cloud-python
# 從 https://dashboard.aspose.cloud/ 獲取客戶端憑據
def PPTXtoODP():
try:
# 初始化 Aspose.Slides Cloud 實例。
configuration = asposeslidescloud.Configuration()
configuration.app_sid = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"
configuration.app_key = "1c9379bb7d701c26cc87e741a29987bb"
slidesApi = asposeslidescloud.SlidesApi(configuration)
# 呼叫 API 將 PPTX 轉換為 ODP 格式並將輸出保存到雲端儲存。
# slides 是一個可選參數,在這個例子中,只有 slide 2 和 3 被轉換。如果跳過,則所有幻燈片都會被轉換。
request = slidesApi.save_presentation(name='test-unprotected.pptx',format='odp',
out_path="ConvertedFile.odp", slides= '2,3')
print("Result {0}".format(request))
except ApiException as e:
print("Exception when calling SlidesApi->save_presentation: %s\n" % e)
請從 test-unprotected.pptx 和 ConvertedFile.odp 下載上述範例中使用的樣本檔案。
從本地磁碟載入PPTX
在這個情境中,輸入的 PPTX 檔案是從本地磁碟載入並轉換為 ODP 格式。與上述情境類似,結果檔案被儲存在雲端儲存空間中。
- 首先創建一個 Configuration 類的物件
- 其次,初始化 SlidesApi 實例,同時將 Configuration 物件作為參數傳遞
- 現在將輸入的 PPTX 加載到檔案物件中
- 最後,呼叫方法 convertandsave(..) 並提供輸入 PPTX、輸出格式為 ODP、輸出路徑細節。在這種情況下,我們不提供可選參數 Slides 的任何值,因此所有幻燈片都會默認轉換。
# 如需更多範例,請造訪 https://github.com/aspose-slides-cloud/aspose-slides-cloud-python
# 從 https://dashboard.aspose.cloud/ 獲取客戶端憑證
def PPTXtoODP():
try:
# 初始化 Aspose.Slides Cloud 實例。
configuration = asposeslidescloud.Configuration()
configuration.app_sid = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"
configuration.app_key = "1c9379bb7d701c26cc87e741a29987bb"
slidesApi = asposeslidescloud.SlidesApi(configuration)
# 呼叫API將PPTX轉換為ODP格式,並將輸出保存到雲端儲存。
# 由於未提供 slides 參數的值,因此所有幻燈片將默認轉換。
request = slidesApi.convert_and_save(document= file,format='odp',out_path="Resultant.odp")
# print response code
print("Result {0}".format(request))
except ApiException as e:
print("Exception when calling SlidesApi->convert_and_save: %s\n" % e)
使用 cURL 命令將 PPTX 文件轉換為 ODP
由於 Aspose.Slides Cloud 的 REST 架構,它也可以通過 cURL 命令訪問。因此,我們也可以使用 Aspose.Slides API 通過 cURL 命令將 PPTX 文件轉換為 ODP 格式。然而,我們首先需要根據您的客戶憑證生成 JWT 訪問令牌。請執行以下命令以生成令牌。
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
現在請執行以下命令以從雲端儲存中加載 PPT 檔案並將其轉換為 ODP 格式。請注意,生成的 ODP 也將保存在雲端儲存中。
curl -v -X PUT "https://api.aspose.cloud/v3.0/slides/test-unprotected.ppt/Odp?outPath=ConvertedFile.odp" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d{}

Image 1:- PPT 轉 ODP 轉換預覽。
可以從 test-unprotected.ppt 下載上述範例中使用的樣本 PPT 檔案,從 ConvertedFile-New.odp 下載結果 ODP 檔案。
結論
在這篇文章中,我們討論了如何使用 Python 代碼片段將 Microsoft PowerPoint (PPTX) 轉換為 OpenDocument 格式的詳細資訊。同樣,我們也學習了使用 cURL 命令將 msPowerpoint 保存為 ODP 的步驟。將 PPTX 轉換為 PDF 也相當簡單。Python SDK 的完整源代碼也可以在 GitHub 上下載。您可以訪問 Programmers Guide 以瞭解有關 API 的其他令人興奮的功能。
相關部落格
請造訪以下連結以瞭解更多有關: