Convert Word to JPG, PNG, SVG Using Python: Step-by-Step Guide

Casie LiuCasie Liu
3 min read

Converting Word documents to images, such as JPG, PNG, or SVG, has many practical uses — from creating thumbnails and enhancing web page presentations to sharing content in a fixed format to prevent layout issues. No matter the purpose, learning how to convert Word to image efficiently is a valuable skill. In this article, we’ll walk you through how to convert Word to JPG, PNG, and SVG using Python, helping you save time and streamline your workflow.

How to Convert Word to JPG or PNG in Python

Firstly, let's look at how to convert Word to JPG or PNG. Whether you need to print a document, insert it into a PowerPoint presentation, or protect its content and layout, converting a Word file to an image format like JPG or PNG is a reliable solution. In this tutorial, we'll demonstrate the process using free Spire.Doc for Python, a user-friendly and powerful Python library.

Steps to convert Word to JPG or PNG in Python:

  • Create an object of the Document class.

  • Read a source Word document from files with the Document.LoadFromFile() method.

  • Loop through all pages in the document.

  • Convert the current page to a bitmap image using the Document.SaveImageToStreams() method.

  • Save these bitmap images as JPG or PNG.

The code example below shows how to convert a Word document to JPG:

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

# Create a Document object
document = Document()

# Load a Word file
document.LoadFromFile("/sample.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('/wordtojpg/ToImage-{0}.jpg'.format(i),'wb') as imageFile:
        imageFile.write(imageStream.ToArray())

document.Close()

Python Convert Word to JPG

💡
To convert a Word document to PNG, simply change the file extension to ".png" in the final step. This method also applies when exporting to BMP format.

How to Convert Word to SVG Using Python in 3 Steps

Unlike bitmap formats like JPG or PNG, SVG is a vector image that can be scaled infinitely without losing clarity or quality. In Python, the method to convert Word to SVG differs from converting to bitmap images. Fortunately, with the Document.SaveToFile() method provided by free Spire.Doc for Python, you can easily complete the conversion in just three simple steps.

Steps to convert Word to SVG in Python:

  • Instantiate a Document object.

  • Load a Word document through the Document.LoadFromFile() method.

  • Save the document as SVG with the Document.SaveToFile() method.

The Python example here shows converting a Word document to SVG:

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

# Create a Document object
document = Document()

# Load a Word file
document.LoadFromFile("/sample.docx")

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

document.Close()

Convert Word Files to SVG Using Python

The Conclusion

With the help of free Spire.Doc for Python, converting Word documents to images like JPG, PNG, or SVG has never been easier. Whatever your purpose is, it will help you to export images from Word files without effort. Hopefully, this guide has made it clear how to convert Word to image formats efficiently and opened up more possibilities for using Word files flexibly in your projects.

FAQs about Converting Word to Images

Q1: How do you convert a Word document to a JPG?

You can simply take a screenshot using the Print Screen (SysRq) key and save it as a JPG file. Alternatively, you can use online conversion tools, but be aware that they may not guarantee document privacy. If you need to process a large number of Word files securely, it's recommended to automate the conversion with Spire.Doc for Python.

Q2: How to convert JPG files to Word?

Since JPG images are not editable, you’ll need an OCR (Optical Character Recognition) tool to extract the text. Once the text is recognized, you can save it as a Word document.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu