When compatibility and size are your main priorities, TXT format is definitely an improvement over the notoriously clunky DOCX. Being able to convert quickly between the two is an important capability for many apps, so we are going to tackle it today. This is going to be a lot faster than you thought it would be. Let’s get started
With pip install, our client can be brought in for our API.
pip install cloudmersive-convert-api-client
Now instantiate an API with a key, followed by calling convert_document_docx_to_txt from that instance.
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.text_formatting_mode = 'text_formatting_mode_example' # str | Optional; specify how whitespace should be handled when converting the document to text. Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases. Default is 'minimizeWhitespace'. (optional)try:# Convert Word DOCX Document to Text (txt)api_response = api_instance.convert_document_docx_to_txt(input_file, text_formatting_mode=text_formatting_mode)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_docx_to_txt: %s\n" % e)
And that’s it for today! That’s right, running this code will provide you with your converted TXT file, simple as that.