I am trying to download an image and insert it to a certain paragraph in the attached document
chapters_generator32940.docx (22.7 KB)
The issue is that the image is inserted in the beginning of the document instead of in a paragraph. Here is my code:
import sys
import aspose.words as aw
import requests
from io import BytesIO
def insert_image_after_paragraph(doc_path, paragraph_number, image_url):
# Create Aspose Words document object
document = aw.Document(doc_path)
builder = aw.DocumentBuilder(document)
# Download the image from the URL
response = requests.get(image_url)
image_bytes = BytesIO(response.content)
builder.move_to_paragraph(paragraph_number - 1, 0)
builder.insert_image(image_bytes)
# Save the document
document.save(doc_path)
# Usage example:
insert_image_after_paragraph("chapters_generator32940.docx", 3, "https://cdn.leonardo.ai/users/e084d054-0537-4095-a7b6-4ca61b2950ac/generations/47a21e29-e31c-49c7-9aa3-3851d7683189/Default_An_illustrative_image_depicting_the_journey_of_Rossi_0.jpg")
In this code I try to insert the image after the 3rd paragraph.
Any help is appreciated.