How to Convert RTF to DOCX in Python

Cloudmersive
2 min readJul 6, 2020

--

I’m just going to come out and say it: RTF format has some severe limitations that can really hamper your style. I have had so many experiences where using RTF prevented me from using certain design choices, or I saved an RTF in the wrong piece of software (I’m looking at you Wordpad!) and it corrupted my document. Nobody has time to deal with this, and the manual solution is not much better, as both RTF and the more robust DOCX format are notorious for being complex when it comes to parsing. But that’s ok, because today I have brought you a shortcut method that will allow you to quickly and conveniently convert as many RTFs to DOCX as you like, all from within Python.

We will be using an API to get this done, so we should install its client as our first order of business.

pip install cloudmersive-convert-api-client

Then we can call our function, which you can see below here in this example:

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 Rich Text Format RTF to DOCX Documentapi_response = api_instance.convert_document_rtf_to_docx(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_rtf_to_docx: %s\n" % e)

Ok, finished. That’s right, it’s really that simple.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet