As a Python developer, you may need to edit Word or Excel documents programmatically. You can update such documents without installing any external application. This article will be focusing on how to edit Word or Excel documents using a REST API.
The following topics shall be covered in this article:
- Document Editor REST API and Python SDK
- Edit Word Document using a REST API
- Edit Excel Sheet using a REST API
Document Editor REST API and Python SDK
For editing Word documents or Excel sheets, I will be using the Python SDK of GroupDocs.Editor Cloud API. It allows you to programmatically edit Word processing documents, Excel sheets, or documents of other supported formats. It also provides .NET, Java, PHP, Ruby, Android, and Node.js SDKs as its document editor family members for the Cloud API.
You can install GroupDocs.Editor-Cloud to your Python project using the following command in the console:
pip install groupdocs_editor_cloud
Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Add your ID and secret in the code as demonstrated below:
Edit Word Document using a REST API
You can edit the Word document by following the simple steps mentioned below:
Upload the Document
First of all, upload the Word document to the Cloud using the code example given below:
As the result, the Word file will be uploaded to Cloud Storage and will be available in the files section of your dashboard.
Edit Word Document in Python
Please follow the steps mentioned below to edit the Word document programmatically.
- Create File API and Edit API instances
- Provide WordProcessingLoadOptions
- Load a file with the Load method of Edit API
- Download HTML document using Download File method of File API
- Edit HTML Document
- Upload HTML back using Upload File method of File API
- Provide WordProcessingSaveOptions to Save in DOCX
- Save HTML back to DOCX using Save method of Edit API
The following code snippet shows how to update a Word document using a REST API.
Download the Updated File
The above code sample will save the edited Word file on the cloud. You can download it using the following code sample:
Edit Excel Sheet using a REST API
Please follow the steps mentioned below to edit Excel sheet programmatically.
- Create File API and Edit API instances
- Provide SpreadsheetLoadOptions
- Load a file with the Load method of Edit API
- Download HTML document using Download File method of File API
- Edit HTML Document
- Upload HTML back using Upload File method of File API
- Provide SpreadsheetSaveOptions to Save in XLSX
- Save HTML back to XLSX using Save method of Edit API
The simple code example given below demonstrates how to update an Excel sheet using a REST API. Please follow the steps mentioned earlier to upload and download a file.
Conclusion
In this article, you have learned how to edit Word documents or Excel sheets on the cloud with Document Editor REST API using Python. You also learned how to programmatically upload the files on the cloud and then download the updated files from the cloud. You can learn more about GroupDocs.Editor Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.