How to Convert ODT to JPG in Python

Today we will be diving into a method for setting up quick and easy conversion between open document ODT files and JPG format. Normally this would require several difficult steps: parsing the ODT, splitting it into individual pages, rendering it, and rasterizing it. Today’s solution, however, will take care of all that hassle for you, letting you achieve the same quality of results after just a few minutes of setting it up.

To begin, we install our API client. This is easy with pip install:

pip install cloudmersive-convert-api-client

Now it’s time to implement some code for calling convert_document_odt_to_jpg. Look through this code example and you will see that the important elements are use of an API key to create an API instance, from which we will be able to call our function.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom 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 ODT Text File to JPG/JPEG image arrayapi_response = api_instance.convert_document_odt_to_jpg(input_file, quality=quality)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_odt_to_jpg: %s\n" % e)

And that’s done! Seriously, it’s that easy.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store