在 Python 中使用 REST API 編輯 Excel 工作表。

Excel 是最流行和使用最廣泛的電子表格應用程序之一。它使我們能夠以表格形式組織、分析和存儲數據。我們可以使用 Python 輕鬆地添加、編輯或刪除 Excel 文件的內容。在本文中,我們將學習如何在 Python 中使用 REST API 編輯 Excel 工作表。

本文應涵蓋以下主題:

Excel 電子表格編輯器 REST API 和 Python SDK

為了修改 XLSX 文件,我們將使用 GroupDocs.Editor Cloud 的 Python SDK API。它允許編輯 支持的格式 的文檔。請在控制台中使用以下命令安裝它:

pip install groupdocs_editor_cloud

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

# 此代碼示例演示如何在代碼中添加您的客戶端 ID 和密碼。
client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
my_storage = ""

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

在 Python 中使用 REST API 編輯 Excel 文件

我們可以按照下面給出的簡單步驟來編輯 Excel 文件:

  1. 上傳XLSX文件到雲端
  2. 編輯 Excel電子表格數據
  3. 下載更新後的文件

上傳文件

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

# 此代碼示例演示如何將 Excel 文件上傳到雲端。
# 創建 API 實例
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

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

因此,上傳的 XLSX 文件將在雲端儀表板的 文件部分 中可用。

在 Python 中編輯 Excel 電子表格數據

我們可以按照以下步驟編輯 Excel 工作表的內容:

  • 首先,創建 FileApiEditApi 的實例。
  • 接下來,提供上傳的 XLSX 文件路徑。
  • 然後,將該文件下載為 HTML 文檔。
  • 接下來,將下載的 HTML 文件作為字符串讀取。
  • 然後,編輯 HTML 並保存更新後的 HTML 文檔。
  • 之後,上傳更新後的 HTML 文件。
  • 最後,使用 EditApi.save() 方法將 HTML 保存回 XLSX。

以下代碼示例展示瞭如何在 Python 中使用 REST API 編輯 Excel 工作表數據。

# 此代碼示例演示如何編輯 Excel 工作表的內容。
# 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.xlsx")

# 定義電子表格加載選項
loadOptions = groupdocs_editor_cloud.SpreadsheetLoadOptions()
loadOptions.file_info = fileInfo

# 提供輸出文件夾路徑 
loadOptions.output_path = "output"
# 提供工作表索引進行編輯
loadOptions.worksheet_index = 0
# 裝入紙張
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions))        

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

with open(htmlFile, 'r') as file:
    html = file.read()

# 編輯一些東西...    
html = html.replace("Welcome", "This is a sample sheet!")

# 將 html 上傳回存儲
with open(htmlFile, 'w') as file:
    file.write(html)

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

# 將 html 保存回 xlsx
saveOptions = groupdocs_editor_cloud.SpreadsheetSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.xlsx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))

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

在 Python 中使用 REST API 編輯 Excel 文件。

下載更新文件

上面的代碼示例會將編輯後的 Excel 文件 (XLSX) 保存在雲端。可以使用以下代碼示例下載它:

# 此代碼示例演示如何下載更新的 Excel 文件。
# API初始化
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

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

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

使用 Python 在 Excel 工作表中添加表格

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

html = html.replace("</TABLE>", """</TABLE> <br/><table style="width: 100%;background-color: #dddddd;border: 1px solid black;">
<caption style=\"font-weight:bold;\"> Persons List</caption>
<tr><th style="background-color: #04AA6D; color: white;">First Name</th><th style="background-color: #04AA6D; color: white;">Last Name</th><th style="background-color: #04AA6D; color: white;">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 在 Excel 電子表格中添加表格。請按照前面提到的步驟上傳和下載文件。

# 此代碼示例演示如何編輯 Excel 工作表和插入新表格。
# 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.xlsx")

# 定義電子表格加載選項
loadOptions = groupdocs_editor_cloud.SpreadsheetLoadOptions()
loadOptions.file_info = fileInfo

# 提供輸出文件夾路徑 
loadOptions.output_path = "output"
# 提供工作表索引進行編輯
loadOptions.worksheet_index = 0
# 裝入紙張
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions))        

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

with open(htmlFile, 'r') as file:
    html = file.read()

# 插入表格  
html = html.replace("</TABLE>", """</TABLE> <br/><table style="width: 100%;background-color: #dddddd;border: 1px solid black;">
<caption style=\"font-weight:bold;\"> Persons List</caption>
<tr><th style="background-color: #04AA6D; color: white;">First Name</th><th style="background-color: #04AA6D; color: white;">Last Name</th><th style="background-color: #04AA6D; color: white;">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)

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

# 將 html 保存回 xlsx
saveOptions = groupdocs_editor_cloud.SpreadsheetSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.xlsx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))

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

使用 Python 在 Excel 工作表中添加表格。

在線試用

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

結論

在本文中,我們了解到:

  • 如何在雲端編輯Excel表格數據;
  • 如何使用 Python 在 Excel 工作表中添加表格;
  • 上傳Excel文件到雲端;
  • 如何從雲端下載更新的 Excel 文件。

此外,您可以使用 文檔 了解有關 GroupDocs.Editor Cloud API 的更多信息。我們還提供了一個 API 參考 部分,讓您可以直接通過瀏覽器可視化我們的 API 並與之交互。如有任何歧義,請隨時在論壇上與我們聯繫。

也可以看看