How to Convert DOCX to JPG in Python

Cloudmersive
2 min readJul 6, 2020

--

If you have ever tried to use Python to create JPG images based on a Word document, you would know that the problem is quite difficult. It requires parsing the somewhat obtuse DOCX format, then rendering it, splitting into its constituent pages, and finally rasterization. That’s a ton of work to write that out in Python. But there is an easier way that will cut right past all of that.

To begin, we will need to install the client for the API that we are going to employ.

pip install cloudmersive-convert-api-client

Once that’s complete, we will be free to set up our API instance and use that to call convert_document_docx_to_jpg.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.quality = 56 # int | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75. (optional)try:# Convert Word DOCX Document to JPG/JPEG image arrayapi_response = api_instance.convert_document_docx_to_jpg(input_file, quality=quality)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_docx_to_jpg: %s\n" % e)

And now you’re done! Easy as that.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet