Hi there,
I am trying to use the aspose.words Cloud API with a python script.
So far I managed to get all paragraphs of a docx.
Get all runs from a paragraph.
Edit the text of a paragraph.
How can I get the edited docx again?
Like how can I save it locally again after editing?
request_run = asposewordscloud.RunUpdate(text='Multilateral Corrected.')
update_request = asposewordscloud.models.requests.UpdateRunOnlineRequest(document=request_document, paragraph_path='sections/0/body/paragraphs/21', run=request_run, index=0)
words_api.update_run_online(update_request)
This is my sample code to update the text of a specific run.
When I run the code It seems to execute without any problems but I don’t know how to access the edited docx afterwards.
Best,
Marvin
@ITMarSpain
Please find below the sample code to save the output file to a local drive.
...
result = words_api.update_run_online(update_request)
#save updated output locally
open('test_result.docx','wb').write(list(result.document.values())[0])
@tilal.ahmad Thank you that is very helpful!! Works without problems
One more question:
I don’t want to edit only one run.
What is best practice to do so?
Do I need to {open the docx → update run → save updated docx} for each run I want to update or is there a better practice to update multiple runs at once?
Also when I extract the text of the runs:
Do I need to loop through each section and paragraphs to get all runs of the docx?
Or is there also an easier method to get the text of each run of the docx?
So far I basically do three loops for my goal:
request_document = open('PATH_TO_DOCX', 'rb')
request = asposewordscloud.models.requests.GetSectionsOnlineRequest(document=request_document)
sections = words_api.get_sections_online(request)
for s in range(0, len(sections.sections.section_link_list)):
request_document = open('PATH_TO_DOCX', 'rb')
request = asposewordscloud.models.requests.GetParagraphsOnlineRequest(document=request_document, node_path=f'sections/{s}')
paragraphs = words_api.get_paragraphs_online(request)
for para in paragraphs.paragraphs.paragraph_link_list:
if '/footnotes/' not in para.link.href:
para_index = para.link.href.split('/')[-1]
request_document = open('PATH_TO_DOCX', 'rb')
request = asposewordscloud.models.requests.GetRunsOnlineRequest(document=request_document, paragraph_path=f'sections/{s}/body/paragraphs/{para_index}')
runs = words_api.get_runs_online(request)
for run in runs.runs.list:
dic[dict_index] = run.text
dict_index += 1
@ITMarSpain
I am afraid one API call will update one run, so you cannot update multiple runs with a single request.
@ITMarSpain
Yes, currently you need to loop through each section and paragraph to get all runs of a Word document. However, we have logged a ticket(WORDSCLOUD-2652) for your requirement to get all the runs of a document. We will keep you updated about the issue resolution progress within this forum thread.