How to Replace Image in Word Documents with Python without Effort


Replacing images is a common need when working with Word documents—whether it’s updating placeholders in a template, swapping diagrams for descriptions, or replacing outdated visuals. While Word itself lacks a built-in image replacement feature, Python offers a flexible solution. In this article, you’ll learn how to replace images in Word documents using Python, making it easier to edit documents efficiently and save valuable time.
How to Replace an Image in Word with a New One Using Python
Replacing images in Word usually means manually deleting the old ones and inserting new ones—which can be tedious and time-consuming, especially when dealing with large documents. Fortunately, Python allows you to automate this process without even opening the file. Whether you’re replacing a company logo or localizing visuals in multilingual reports, Python makes image replacement quick and effortless.
In this series, we'll use the Spire.Doc library, one of the most popular and versatile Python libraries for editing Word files, including replacing images or text.
Steps to replace an image in Word documents:
Create an object of the Document class.
Read a source Word document from files using the Document.LoadFromFile() method.
Loop through all sections, each paragraph and each child object.
Check if the picture is DocPicture, if so, append it to a list.
Get a picture from the list and replace it with a new picture through the DocPicture.LoadImage() method.
Save the updated Word document as a new one with the Document.SaveToFile() method.
The Python code here shows how to replace the first picture in a Word file:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
doc = Document()
# Load a Word document
doc.LoadFromFile("/sample.docx")
# Create a list to store the images
pictures = []
# Iterate through all sections in the document
for i in range(doc.Sections.Count):
sec = doc.Sections.get_Item(i)
# Iterate through all paragraphs in each section
for j in range(sec.Paragraphs.Count):
para = sec.Paragraphs.get_Item(j)
# Iterate through all child objects in each paragraph
for k in range(para.ChildObjects.Count):
docObj = para.ChildObjects.get_Item(k)
# Find the images and add them to the list
if docObj.DocumentObjectType == DocumentObjectType.Picture:
pictures.append(docObj)
# Replace the first picture in the list with a new image
picture = pictures[0] if isinstance(pictures[0], DocPicture) else None
picture.LoadImage("/image.png")
# Save the result document
doc.SaveToFile("/ReplaceImage.docx", FileFormat.Docx)
doc.Close()
How to Replace Images with Text in Word Using Python
When exporting a Word document to a plain text format like .txt, it’s often necessary to replace images with descriptive text to preserve the meaning and structure of the document. In other cases, the original image may fail to load, and a text placeholder becomes essential. With Spire.Doc, you can automate this by inserting replacement text at the image's position and then deleting the image. Here’s how to do it with just a few lines of Python code.
Steps to replace images with text in a Word document:
Create a Document instance and read a Word document with the Document.LoadFromFile() method.
Iterate through all sections, paragraphs and child objects.
Find images and add them to a list.
Loop through images in the list.
Get the index of a specified image in a specified paragraph using the Paragraph.ChildObjects.Indexof() method.
Initialize an object of the TextRange class and set text for the text range through the TextRange.Text property.
Insert the text range at the position of a picture with the Paragraph.ChildObjects.Insert() method.
Remove images from the document using the Paragraph.ChildObjects.Remove() method.
Save the modified Word document.
from spire.doc import *
from spire.doc.common import *
# Create a Document object
doc = Document()
# Load a Word document
doc.LoadFromFile("/sample.docx")
j = 1
# Iterate through all sections in the document
for k in range(doc.Sections.Count):
sec = doc.Sections.get_Item(k)
# Iterate through all sections in the document
for m in range(sec.Paragraphs.Count):
para = sec.Paragraphs.get_Item(m)
# Create a list to store the images
pictures = []
# Find the images and add them to the list
for x in range(para.ChildObjects.Count):
docObj = para.ChildObjects.get_Item(x)
if docObj.DocumentObjectType == DocumentObjectType.Picture:
pictures.append(docObj)
# Iterate through all images in the list and replace them with text "Here is image {image index}"
for pic in pictures:
index = para.ChildObjects.IndexOf(pic)
textRange = TextRange(doc)
textRange.Text = "Here is image {0}".format(j)
para.ChildObjects.Insert(index, textRange)
para.ChildObjects.Remove(pic)
j += 1
# Save the result document
doc.SaveToFile("/ReplaceWithText.docx", FileFormat.Docx)
doc.Close()
The Bottom Line
Whether you're looking to replace an image in a Word document with a new one, or swap out pictures for text, Python provides an efficient solution. With the help of libraries like Spire.Doc, you can quickly handle bulk replacements without manually opening the file. Hopefully, this guide has helped you to replace images in Word using Python and inspired you to streamline your Word document editing tasks.
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
