Issue inserting an image to a paragraph

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.

@ansar2024 The shape is properly inserted into the third paragraph in your document:

# Create Aspose Words document object
document = aw.Document("C:\\Temp\\in.docx")
builder = aw.DocumentBuilder(document)

builder.move_to_paragraph(2, 0)
builder.insert_image("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")

# Save the document
document.save("C:\\Temp\\out.docx")

I see. Thank you very much for this indication. From where can I download the Document Explorer tool?

@ansar2024 You can get DocumentExplorer sources from our Github . This is a .NET application. There is also Java version.

1 Like