How to convert PDF to Word DOCX format in Python
On today’s agenda: conversion between PDF format and DOCX. While this is quite a headache to set up in the standard way, we have a nice solution for the problem that won’t take up more than a few minutes of your time. Let’s take a look.
Pip installation of our API client is first.
pip install cloudmersive-convert-api-client
Now we are going to call convert_document_pdf_to_docx, which is demonstrated here.
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 PDF to Word DOCX Documentapi_response = api_instance.convert_document_pdf_to_docx(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_pdf_to_docx: %s\n" % e)
And finally… well that’s it really. Just input the file and you will get your PDF, simple as that. For over 200 other useful productivity-related functions like this, take a look at the documentation for the rest of this library.