How to Insert an Image into a DOCX File in Python

While on your DOCX editing journey, you may need to add an image into the document. If so, you can open up the file for editing with our Begin Editing function, and use the following function to insert the image. Executing this quick and easy process will take the pain out of editing, and provide you with a quality result.

Image for post
Image for post

Before we get started, you want to make sure you have the information for the following request configuration ready to go:

"InputFileBytes": "string",
"InputFileUrl": "string",
"ParagraphToInsert": {
"ParagraphIndex": 0,
"Path": "string",
"ContentRuns": [
"RunIndex": 0,
"Path": "string",
"TextItems": [
"TextIndex": 0,
"Path": "string",
"TextContent": "string"
"Bold": true,
"Italic": true,
"Underline": "string",
"FontFamily": "string",
"FontSize": "string"
"StyleID": "string"
"InsertPlacement": "string",
"InsertPath": "string"

Now, we can run this command to install the SDK:

pip install cloudmersive-convert-api-client

Then, we can call the function:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from import ApiException
from pprint import pprint

This will successfully add the image to your Word document, and return your edited URL. If you have other editing operations to perform, you can move on to those. Alternatively, if you have no further changes, you can use the Finish Editing API to solidify your changes and download your updated document.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store