How to convert Word DOCX to PDF in Python

Converting a document into a PDF is a breeze with our conversion API. Let’s look at just how easy this is. First, use the pip installer to add our API client:

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Convert.git

Then we shall call convert_document_autodetect_to_pdf and input the desired document.

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = 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 class
api_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 Document to PDF
api_response = api_instance.convert_document_autodetect_to_pdf(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_autodetect_to_pdf: %s\n" % e)

How easy was that! Our API will automatically detect the file format and perform the conversion to PDF. This function works on many other formats, including Word and Excel files, as well as over 100 image formats.

Image for post
Image for post

Written by

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

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