Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active November 19, 2021 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/5024c86490677f4cb2ac3b099106a0be to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/5024c86490677f4cb2ac3b099106a0be to your computer and use it in GitHub Desktop.
Edit Word Documents using REST API in Python
# Initialize the instances of the APIs
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)
# Provide the input file path
fileInfo = groupdocs_editor_cloud.FileInfo("sample.docx")
# Define LoadOptions to load it into editable state
loadOptions = groupdocs_editor_cloud.WordProcessingLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
# Create load request
loadRequest = groupdocs_editor_cloud.LoadRequest(loadOptions)
# Load
loadResult = editApi.load(loadRequest)
# Create download request
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)
# Download html document
htmlFile = fileApi.download_file(downloadRequest)
# Read the html document
html = ""
with open(htmlFile, 'r') as file:
html = file.read()
# Insert an image
html = html.replace("left-aligned.", """left-aligned. <br/> <img src=\"groupdocs.png\" alt=\"signatures\" style=\"width: 128px; height: 128px;\">""");
# Write html back to file
with open(htmlFile, 'w') as file:
file.write(html)
# Create upload request
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)
# Upload file
fileApi.upload_file(uploadRequest)
# Save html back to docx
saveOptions = groupdocs_editor_cloud.WordProcessingSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/add_image.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
# Create save request
saveRequest = groupdocs_editor_cloud.SaveRequest(saveOptions)
# Save
saveResult = editApi.save(saveRequest)
# Done
print("Document edited: " + saveResult.path)
# Initialize the instances of the APIs
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)
# Provide the input file path
fileInfo = groupdocs_editor_cloud.FileInfo("sample.docx")
# Define LoadOptions to load it into editable state
loadOptions = groupdocs_editor_cloud.WordProcessingLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
# Create load request
loadRequest = groupdocs_editor_cloud.LoadRequest(loadOptions)
# Load
loadResult = editApi.load(loadRequest)
# Create download request
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)
# Download html document
htmlFile = fileApi.download_file(downloadRequest)
# Read the html document
html = ""
with open(htmlFile, 'r') as file:
html = file.read()
# Insert table
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>""")
# Write html back to file
with open(htmlFile, 'w') as file:
file.write(html)
# Create upload request
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)
# Upload file
fileApi.upload_file(uploadRequest)
# Save html back to docx
saveOptions = groupdocs_editor_cloud.WordProcessingSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/add_table.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
# Create save request
saveRequest = groupdocs_editor_cloud.SaveRequest(saveOptions)
# Save
saveResult = editApi.save(saveRequest)
# Done
print("Document edited: " + saveResult.path)
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 = ""
# API initialization
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_editor_cloud.DownloadFileRequest("output/edited.docx", my_storage)
# Download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\Editor\\")
# Initialize the instances of the APIs
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)
# Provide the input file path
fileInfo = groupdocs_editor_cloud.FileInfo("sample.docx")
# Define LoadOptions to load it into editable state
loadOptions = groupdocs_editor_cloud.WordProcessingLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
# Create load request
loadRequest = groupdocs_editor_cloud.LoadRequest(loadOptions)
# Load
loadResult = editApi.load(loadRequest)
# Create download request
downloadRequest = groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path)
# Download html document
htmlFile = fileApi.download_file(downloadRequest)
# Read the html document
html = ""
with open(htmlFile, 'r') as file:
html = file.read()
# Edit something...
html = html.replace("Title of the document", "Hello world")
html = html.replace("Subtitle #1", "Welcome")
# Write html back to file
with open(htmlFile, 'w') as file:
file.write(html)
# Create upload request
uploadRequest = groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile)
# Upload file
fileApi.upload_file(uploadRequest)
# Save html back to docx
saveOptions = groupdocs_editor_cloud.WordProcessingSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "output/edited.docx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
# Create save request
saveRequest = groupdocs_editor_cloud.SaveRequest(saveOptions)
# Save
saveResult = editApi.save(saveRequest)
# Done
print("Document edited: " + saveResult.path)
# Create an instance of the API
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)
# Upload sample files
request = groupdocs_editor_cloud.UploadFileRequest("sample.docx", "C:\\Files\\Editor\\sample.docx", my_storage)
response = file_api.upload_file(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment