Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Edit PowerPoint Presentation using a REST API in Python
Edit PowerPoint Presentation
1. Programmatically upload a PPTX file on the cloud
2. Edit PowerPoint Presentation using a REST API in Python
3. Download updated PPTX from Cloud
client_id = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5"
client_secret = "479db2b01dcb93a3d4d20efb16dea971"
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)
# download the file
request = groupdocs_editor_cloud.DownloadFileRequest("edited.pptx", my_storage)
response = file_api.download_file(request)
# move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# api initialization
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)
# load it into editable state
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadOptions.show_hidden_slides = True
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions))
# download html document
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""
# read HTML file
with open(htmlFile, 'r') as file:
html = file.read()
# replace text
html = html.replace("Hello World", "Welcome")
# upload HTML back to storage
with open(htmlFile, 'w') as file:
file.write(html)
fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))
# save HTML back to PPTX
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveOptions.password = "password"
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))
# done
print("Document edited: " + saveResult.path)
# api initialization
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)
# load it into editable state
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions))
# download html document
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""
# read HTML file
with open(htmlFile, 'r') as file:
html = file.read()
# upload Image to replace
request = groupdocs_editor_cloud.UploadFileRequest(loadOptions.output_path + "/sample.files/groupdocs.png", "C:\\Files\\groupdocs.png", "")
response = fileApi.upload_file(request)
# replace Image
html = html.replace("Picture 2.png", "groupdocs.png")
# upload HTML back to storage
with open(htmlFile, 'w') as file:
file.write(html)
fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))
# save HTML back to PPTX
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))
# create instance of the api
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)
# upload sample file
request = groupdocs_editor_cloud.UploadFileRequest("sample.pptx", "C:\\Files\\sample.pptx", 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