Convert Image to Word in Python

MS Word DOC and DOCX formats are widely used to create rich text documents. You can format text and insert various elements in Word documents such as images. In certain cases, you have to convert a single image or a bunch of images to a Word document. To perform this operation programmatically, this article shows how to convert images to Word DOC DOCX in Python.

Python Image to Word Converter Library - Free Download

For the image to Word DOC/DOCX conversion, we will use Aspose.Words for Python. It is a feature-rich library to work with Word documents including DOC, DOCX, etc. The library has a built-in converter for back-and-forth conversion of Word documents. You can install the library from PyPI using the following pip command.

> pip install aspose-words

Convert an Image to Word DOC in Python

The following are the steps to convert an image to a Word DOC in Python.

  • First, create a new document using the Document class.
  • Then, create a DocumentBuilder object and initialize it with the Document object.
  • Insert image into the document using DocumentBuilder.insert_image(fileName) method.
  • Finally, save the Word document using Document.save(fileName) method.

The following code sample shows how to convert a PNG image to DOC in Python.

Convert Multiple Images to Word in Python

In the previous section, we converted only a single image to Word document. However, in certain cases, you may need to convert more than one image at once. The following are the steps to convert multiple images to a Word DOCX in Python.

  • First, create a new document using the Document class.
  • Then, create a DocumentBuilder object and initialize it with the Document object.
  • Get the list of the image files from the desired folder.
  • Loop through the list of the image files and insert each image into the document using DocumentBuilder.insert_image(fileName) method.
  • Finally, save the Word document using Document.save(fileName) method.

The following code sample shows the conversion of multiple images to a Word DOCX in Python.

Image to DOC Python Converter - Get a Free License

You can convert images to Word formats without evaluation limitations by getting a free temporary license.

Python Image to DOCX Converter - Read More

You can explore more about Aspose.Words for Python using documentation. In case you would have any queries, feel free to let us know via our forum.

Conclusion

In this article, you have learned how to convert an image to a Word document in Python. We also demonstrated how to convert multiple images to a Word DOC/DOCX document programmatically. You can easily integrate the provided code samples into your application and perform the image to DOC or DOCX conversion.

See Also