Convert Word Doc or Docx to Images with Python

Alexander Stock
3 min readDec 25, 2023

--

Python Word to Image

Converting Word documents into images provides a powerful means to enhance communication and captivate audiences through visually appealing content. Moreover, converting Word to images facilitates seamless integration with various mediums and platforms, including social media, presentations, websites, and digital publications.

In this article, I am going to introduce how to convert Word (Doc or Docx) into popular image file formats such as PNG, JPG and SVG in Python.

Python Library for Converting Word to Images

This solution relies on Spire.Doc for Python, which is a powerful library that enables developers to create, manipulate, and convert Word documents programmatically in Python applications. It provides a comprehensive set of APIs and features, allowing you to generate, modify, and extract content from Word documents.

With Spire.Doc, you can create new Word documents from scratch, load existing documents, perform various document operations such as adding or deleting sections, paragraphs, tables, images, headers, footers, and more. In addition to document creation and manipulation, Spire.Doc allows you to convert Word documents to different formats such as PDF, HTML, RTF, XML, and image files.

You can easily install Spire.Doc for Python in your Python project by running the following pip command.

pip install spire.doc

Convert Word Doc or Docx to PNG in Python

The following code initiates an instance of Document class and loads a Word file using LoadFromFile method. Then, it iterates through the pages in the document and converts each page into a bitmap image using SaveImageToStreams method. Lastly, each bitmap image is saved as a separate PNG file.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a doc or docx file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Loop through the pages in the document
for i in range(document.GetPageCount()):

# Convert a specific page to bitmap image
imageStream = document.SaveImageToStreams(i, ImageType.Bitmap)

# Save the bitmap to a PNG file
with open('Output/ToImage-{0}.png'.format(i),'wb') as imageFile:
imageFile.write(imageStream.ToArray())

document.Close()

Convert Word Doc or Docx to JPG in Python

The code for converting Word to JPG is almost the same as the code for converting Word to PNG, the only difference is that you need to save the bitmap image in JPG format.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a doc or docx file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Loop through the pages in the document
for i in range(document.GetPageCount()):

# Convert a specific page to bitmap image
imageStream = document.SaveImageToStreams(i, ImageType.Bitmap)

# Save the bitmap to a JPG file
with open('Output/ToImage-{0}.jpg'.format(i),'wb') as imageFile:
imageFile.write(imageStream.ToArray())

document.Close()

Convert Word Doc or Docx to SVG in Python

To convert Word into SVG files, you could simply load a Word file and convert it to multiple SVG files using SaveToFile method. The following code gives you an example.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a doc or docx file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")

# Convert it to SVG files
document.SaveToFile("output/ToSVG.svg", FileFormat.SVG)

document.Close()

See Also

Convert Word to PDF in Python

Convert Word to HTML and Vice Versa in Python

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.