在 Python 中使用 REST API 編輯 Word 文檔

在某些情況下,我們可能需要以編程方式編輯 Word 文檔。我們可以輕鬆地添加、編輯或刪除 DOC 或 DOCX 文件的內容,或者使用 Python 應用文本格式。在本文中,我們將學習如何在 Python 中使用 REST API 編輯 Word 文檔。

本文應涵蓋以下主題:

Word 文檔編輯器 REST API 和 Python SDK

為了編輯 DOCX 文件,我們將使用 GroupDocs.Editor Cloud 的 Python SDK API。請在控制台中使用以下命令安裝它:

pip install groupdocs_editor_cloud

在執行上述步驟之前,請從儀表板獲取您的客戶端 ID 和密碼。獲得 ID 和密碼後,添加如下所示的代碼:

client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"

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

在 Python 中使用 REST API 編輯 Word 文檔

我們可以按照下面提到的簡單步驟來編輯Word文檔:

  1. 上傳DOCX文件到雲端
  2. 編輯 使用 Python 的 Word 文檔
  3. 下載更新文件

上傳文件

首先,我們將使用下面給出的代碼示例將 DOCX 文件上傳到雲端:

# 創建 API 實例
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

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

因此,上傳的 DOCX 文件將在雲端儀錶盤的 文件部分 中可用。

使用 Python 編輯 Word 文檔

我們可以按照以下步驟以編程方式編輯 Word 文檔:

  • 首先,創建 FileApiEditApi 的實例。
  • 接下來,創建 FileInfo 的實例並提供輸入 DOCX 文件路徑。
  • 然後,初始化 WordProcessingLoadOptions 的實例並分配 FileInfo。
  • 接下來,使用 WordProcessingLoadOptions 對像作為參數創建 LoadRequest。
  • 然後,使用 LoadRequest 對象調用 EditApi.load() 方法以加載輸入 DOCX 文件。
  • 之後,使用加載的文件創建 DownloadFileRequest。
  • 然後,調用 FileApi.download\file() 方法將文件下載為 HTML 文檔。
  • 接下來,將下載的 HTML 文件作為字符串讀取。
  • 然後,編輯 HTML 並保存更新後的 HTML 文檔。
  • 接下來,創建 UploadFileRequest 並將 HTML 路徑和文件作為參數傳遞。
  • 然後,使用 UploadFileRequest 調用 FileApi.upload\file() 方法來上傳更新後的 HTML 文件。
  • 接下來,創建 WordProcessingSaveOptions 的實例以保存在 DOCX 中。
  • 之後,使用 WordProcessingSaveOptions 對象創建 SaveRequest。
  • 最後,使用帶有 SaveRequest 對象的 EditApi.save() 方法將 HTML 保存回 DOCX。

以下代碼示例展示瞭如何在 Python 中使用 REST API 編輯 Word 文檔。

# 初始化 API 的實例
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.docx")

# 定義 加載Options 以將其加載到可編輯狀態
loadOptions = groupdocs_editor_cloud.WordProcessing加載Options()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"

# 創建加載請求
loadRequest = groupdocs_editor_cloud.加載Request(loadOptions)

# 加載
loadResult = editApi.load(loadRequest)        

# 創建下載請求
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)

# 下載 html 文件
htmlFile = fileApi.download_file(downloadRequest)

# 閱讀html文檔
html = ""       
with open(htmlFile, 'r') as file:
    html = file.read()

# 編輯一些東西...    
html = html.replace("Title of the document", "Hello world")
html = html.replace("Subtitle #1", "Welcome")

# 將 html 寫回文件
with open(htmlFile, 'w') as file:
    file.write(html)

# 創建上傳請求
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)

# 上傳文件
fileApi.upload_file(uploadRequest)

# 將 html 保存回 docx
saveOptions = groupdocs_editor_cloud.WordProcessing節省Options()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/edited.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path

# 創建保存請求
saveRequest = groupdocs_editor_cloud.節省Request(saveOptions)

# 節省
saveResult = editApi.save(saveRequest)

# 完畢
print("Document edited: " + saveResult.path)
在 Python 中使用 REST API 編輯 Word 文檔。

在 Python 中使用 REST API 編輯 Word 文檔。

下載更新文件

以上代碼示例將編輯後的 Word 文檔 (DOCX) 保存在雲端。可以使用以下代碼示例下載它:

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

# 創建下載文件請求
request = groupdocs_editor_cloud.DownloadFileRequest("output/edited.docx", my_storage)

# 下載文件
response = file_api.download_file(request)

# 將下載的文件移動到您的工作目錄
shutil.move(response, "C:\\Files\\Editor\\")

使用 Python 在 Word 文檔中添加表格

我們可以按照前面提到的步驟以編程方式在 Word 文檔中添加表格。但是,我們需要更新 HTML 以在文檔中添加一個表格,如下所示:

html = html.replace("left-aligned.", """left-aligned. <br/><table style="width: 100%;background-color: #dddddd;">
	<caption style=\"font-weight:bold;\"> Persons List</caption>
	<tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>
	<tr><td>Jill</td><td>Smith</td><td>50</td></tr>
	<tr><td>Eve</td><td>Jackson</td><td>94</td></tr>
	</table>""")

以下代碼示例展示瞭如何在 Python 中使用 REST API 在 Word 文檔中添加表格。請按照前面提到的步驟上傳和下載文件。

# 初始化 API 的實例
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.docx")

# 定義 加載Options 以將其加載到可編輯狀態
loadOptions = groupdocs_editor_cloud.WordProcessing加載Options()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"

# 創建加載請求
loadRequest = groupdocs_editor_cloud.加載Request(loadOptions)

# 加載
loadResult = editApi.load(loadRequest)        

# 創建下載請求
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)

# 下載 html 文件
htmlFile = fileApi.download_file(downloadRequest)

# 閱讀html文檔
html = ""       
with open(htmlFile, 'r') as file:
    html = file.read()

# 插入表格
html = html.replace("left-aligned.", """left-aligned. <br/><table style="width: 100%;background-color: #dddddd;">
  <caption style=\"font-weight:bold;\"> Persons List</caption>
  <tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>
  <tr><td>Jill</td><td>Smith</td><td>50</td></tr>
  <tr><td>Eve</td><td>Jackson</td><td>94</td></tr>
  </table>""")

# 將 html 寫回文件
with open(htmlFile, 'w') as file:
    file.write(html)

# 創建上傳請求
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)

# 上傳文件
fileApi.upload_file(uploadRequest)

# 將 html 保存回 docx
saveOptions = groupdocs_editor_cloud.WordProcessing節省Options()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/add_table.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path

# 創建保存請求
saveRequest = groupdocs_editor_cloud.節省Request(saveOptions)

# 節省
saveResult = editApi.save(saveRequest)

# 完畢
print("Document edited: " + saveResult.path)
使用 Python 在 Word 文檔中添加表格。

使用 Python 在 Word 文檔中添加表格。

使用 Python 在 Word 文檔中插入圖像

我們可以按照前面提到的步驟以編程方式在 Word 文檔中插入圖像。但是,我們需要更新 HTML 以在文檔中插入圖像,如下所示:

html = html.replace("left-aligned.", """left-aligned. <br/> <img src=\"groupdocs.png\" alt=\"signatures\" style=\"width: 128px; height: 128px;\">""");

以下代碼示例展示瞭如何在 Python 中使用 REST API 在 Word 文檔中插入圖像。請按照前面提到的步驟上傳和下載文件。

# 初始化 API 的實例
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.docx")

# 定義 加載Options 以將其加載到可編輯狀態
loadOptions = groupdocs_editor_cloud.WordProcessing加載Options()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"

# 創建加載請求
loadRequest = groupdocs_editor_cloud.加載Request(loadOptions)

# 加載
loadResult = editApi.load(loadRequest)        

# 創建下載請求
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)

# 下載 html 文件
htmlFile = fileApi.download_file(downloadRequest)

# 閱讀html文檔
html = ""       
with open(htmlFile, 'r') as file:
    html = file.read()

# 插入圖像
html = html.replace("left-aligned.", """left-aligned. <br/> <img src=\"groupdocs.png\" alt=\"signatures\" style=\"width: 128px; height: 128px;\">""");

# 將 html 寫回文件
with open(htmlFile, 'w') as file:
    file.write(html)

# 創建上傳請求
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)

# 上傳文件
fileApi.upload_file(uploadRequest)

# 將 html 保存回 docx
saveOptions = groupdocs_editor_cloud.WordProcessing節省Options()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/add_image.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path

# 創建保存請求
saveRequest = groupdocs_editor_cloud.節省Request(saveOptions)

# 節省
saveResult = editApi.save(saveRequest)

# 完畢
print("Document edited: " + saveResult.path)
在 Python 中使用 REST API 編輯 Word 文檔

使用 Python 在 Word 文檔中插入圖像。

在線試用

請試用以下使用上述API開發的免費在線DOCX編輯工具。 https://products.groupdocs.app/editor/docx

結論

在本文中,我們學習瞭如何在雲端編輯 Word 文檔。我們還了解瞭如何使用 Python 中的 REST API 在 DOCX 文件中添加表格或插入圖像。本文還介紹瞭如何以編程方式將 DOCX 文件上傳到雲端,然後從雲端下載編輯後的文件。此外,您可以使用 文檔 了解有關 GroupDocs.Editor Cloud API 的更多信息。我們還提供了一個 API 參考 部分,讓您可以直接通過瀏覽器可視化我們的 API 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。

也可以看看