
使用 Python SDK 將 ODT 轉換為 Word
本文是我們先前的文章 使用 Java REST API 將 ODT 轉換為 DOC 的續篇,我們將討論如何使用 Python SDK 將 ODT 轉換為 Word 的細節。我們將開放文件格式轉換為 MS Word 格式的原因是該格式在使用者中具有廣泛的接受度和適應性。許多應用程式使用這種格式作為事實上的標準,並廣泛用於大學、研究所、政府組織等。
DOC 轉換 API
為了在 Python 應用程式中將 ODT 轉換為 Word,我們需要使用 Aspose.Words Cloud SDK for Python。它能夠編輯、操作和將 Word 文件轉換為各種 支援的格式。現在請執行以下指令來安裝SDK:
pip install aspose-words-cloud
另一個重要步驟是透過存取 Aspose.Cloud 儀表板 建立一個免費帳戶。它使我們能夠存取雲端 API 以及管理雲端儲存中的文件。
使用 Python 將 ODT 轉換為 Word
請按照下面給出的說明使用 Python 程式碼片段將 ODT 轉換為 Word。
- 首先,建立一個 WordsApi 對象,並傳遞客戶詳細資訊作為參數。
- 其次,使用 uploadfile(…) 方法將輸入的 ODT 檔案上傳到雲端儲存。
- 現在建立一個 GetDocumentWithFormatRequest(…) 的實例,它以輸入 ODT 檔案名稱、結果檔案格式和輸出檔案名稱作為參數。
- 最後,呼叫 getdocumentwithformat(…) 方法,該方法以 GetDocumentWithFormatRequest 物件作為參數。
- 產生的 Word 文件(DOCX)保存在雲端儲存中。
// 有關更多範例,請造訪 https://github.com/aspose-words-cloud/aspose-words-cloud-python
def ODTtoWord():
try:
# 建立 WordsApi 實例
words_api = WordsApi("bbf94a2c-6d7e-4020-b4d2-b9809741374e","1c9379bb7d701c26cc87e741a29987bb")
# 輸入 ODT 文件的名稱
inputFileName = 'TableDocument.odt'
# 產生的 Word 文件的名稱
resultantFile = 'Resultant.docx'
# 將來源 Word 文件上傳到雲端存儲
words_api.upload_file(asposewordscloud.models.requests.UploadFileRequest(open('C:\\Users\\Downloads\\'+inputFileName, 'rb'), "", None))
# 建立一個用於文檔轉換的物件。指定輸出格式為 DOCX
request = GetDocumentWithFormatRequest(inputFileName, "DOCX", None, None, None, None, resultantFile, None)
# 啟動 ODT 到 DOCX 的轉換操作
result = words_api.get_document_with_format(request)
# 在控制台中列印訊息(可選)
print('Open document sucessfully converted to DOCX !')
except ApiException as e:
print("Exception while calling WordsApi: {0}".format(e))

圖:1- ODT 到 Word 轉換預覽
使用 cURL 指令將 ODT 轉換為 DOCX
我們也可以使用 cURL 指令將 ODT 轉換為 DOCX 格式。我們需要做的是首先根據客戶端憑證產生一個 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"
現在我們有了 JWT 令牌,我們需要執行以下命令將 ODT 儲存為 DOCX 格式。
curl -v -X GET "https://api.aspose.cloud/v4.0/words/TableDocument.odt?format=DOCX&outPath=resultant.docx" \
-H "accept: application/octet-stream" \
-H "Authorization: Bearer <JWT Token>"
結論
我們已經了解如何使用 Python 程式碼片段以及透過 cURL 命令以程式設計方式將 ODT 轉換為 DOCX 的詳細資訊。除了這種轉換之外,您還可以執行開發人員指南中提到的大量功能。如果您在使用 API 時遇到任何問題,請隨時透過技術支援論壇聯絡我們。
相關文章
我們強烈建議您閱讀以下文章以了解更多資訊: