How to convert a Word DOCX to a PNG Array in Python
Today’s post is all about proving that old adage “pictures speak louder than words.” To make this happen, we will be using Python to convert from a DOCX document file into a lovely array of PNG images. Instead of using the old fashioned way, which takes forever, we will be saving a boatload of time with the aid of an API. Let’s get started!
Pip installation is our first order of business, like you see here:
pip install cloudmersive-convert-api-client
Now we are able to call our function, which is easy with this example code to follow.
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.try:# Convert DOCX document to PNG image arrayapi_response = api_instance.convert_document_docx_to_png(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_docx_to_png: %s\n" % e)
Boom! Done. Your resulting image array will be returned as part of a JSON object. Easy.