|
# 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) |