Technical Articles

Review Cloudmersive's technical library.

How to Convert Excel XLSX to PDF in Python
9/22/2025 - Brian O'Neill


File format conversion APIs can make enterprise applications easier to build, help them run faster, and allow them to deliver a higher quality product to end users and stakeholders. That’s especially true when we’re looking to convert popular files from the MS Office suite to the universally accessible Portable Document Format (PDF).

In this article, we’ll learn how to seamlessly automate conversions from Excel XLSX to PDF format in Python using the Cloudmersive Convert API. We’ll make a REST API call with the Convert API SDK by taking advantage of complementary, copy-and-paste Python code examples provided in the API documentation. By the time we’re done, we’ll be experts at leveraging Cloudmersive documentation and managing Cloudmersive API responses – and we’ll have an easy-to-use tool in our belt for publishing spreadsheets at scale.

This article will provide screenshots for an example conversion workflow in Google Colab along with code snippets we can copy & paste from directly as we follow along.

Creating a Cloudmersive account

Before we get started, it’s important to note that Cloudmersive APIs use API key authorization. If we don’t currently have a Cloudmersive account, we should sign up for a free account before we follow along with the rest of this article. That’ll give us an API key which never expires and allows a limit of 800 API calls per month. We can use this key indefinitely to implement and test dozens of different Cloudmersive APIs in our Python applications.

Locating Convert API code examples

Once we’ve created our Cloudmersive account, we’ll find the code examples we need by navigating to the API Console page from our Cloudmersive Management Portal (CMP).

1 - find api console link

After we reach this page, we’ll click on the Convert API link. This will bring us to a Swagger UI page displaying Convert API services.

1a - select conver api console page
3 - convert api page

From here, we’ll scroll down until we find the ConvertDocument path. On this path, we’ll locate the Convert Excel XLSX Spreadsheet to PDF service.

4 - show convert document path
5 - find excel to pdf api

Clicking on the Convert Excel XLSX Spreadsheet to PDF tab will reveal the API specification. This includes important details we’ll want to remember – like the API response model, for instance, which returns a string of Excel file bytes.

6 - review excel to pdf api tab

Below the response model, we’ll click on the Python tab to display code examples for the Convert API Python SDK. This will reveal Convert API library imports pre-structured for our convenience along with code snippets for handling authorization, class instantiation, and response handling.

7 - review code examples
7a - review code examples

Selecting the Install Python SDK dropdown will reveal the pip command we need to install Convert API resources in our environment.

8 - reveal pip install

To begin implementing this API in our environment, we simply need to run the pip command in our terminal (or code cell if we’re using Colab/Jupyter Notebooks) and paste the remaining code in our Python file.

9 - run command in code cell
10 - paste code examples in colab

We’ll find the exact code shown above in the below code block:

pip install cloudmersive-convert-api-client


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'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.

try:
    # Convert Excel XLSX Spreadsheet to PDF
    api_response = api_instance.convert_document_xlsx_to_pdf(input_file)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ConvertDocumentApi->convert_document_xlsx_to_pdf: %s\n" % e)

Structuring our Excel to PDF conversion request

Now that we’ve located and implemented Python code examples, we’ll structure and execute a rudimentary Excel to PDF conversion request, and we’ll save the resulting PDF on our local path.

Configuring our API key

First, we’ll replace the ‘YOUR_API_KEY’ placeholder text with our actual API key string. In case we haven’t found our API key yet, it’s in the API Keys section of our CMP home page. We can copy and paste our key directly into our code from there – but it’s best practice to obfuscate secret keys in our code. In this example workflow, we’ll do so by storing our key in the Secrets tab in Colab and then using the provided import & method to set our key at runtime.

11 - configure API key

Configuring our file input

Next, we’ll replace the ‘/path/to/inputfile’ placeholder string with a test file we upload on our local path. In this example, we’ll be converting a spreadsheet titled Annual Expenditure.xlsx which tracks a (fake) company’s spending over a multi-year period.

12 - configure file input
12a - show example file

Writing our resulting PDF bytes to a new file

Finally, we’ll remove the pprint(api_response) line at the bottom of our code and replace it with code that writes our API response to a new PDF document. The simplest approach is to create a string variable with our file name and use a with open() statement to write the API response to that file.

Before we do that, however, we’ll first recall that our API response is a string containing our PDF file bytes. This means it’s possible our file will return as a Python code literal rather than as a PDF object directly – and we should handle this gracefully in our code.

In this example, we’ll import the Abstract Syntax Trees (ast) library and use the .literal_eval() method to safely evaluate our API response string back to a PDF object. We’ll use the .strip() string method for good measure just in case any whitespace appeared where it shouldn’t.

13 - write response to new file

Running our code

Now that we’ve fully structured our API call, we’ll run it and confirm our PDF is generated correctly.

14 - show pdf in files tab

We’ll then open our PDF and make sure we’re comfortable with the output.

As a quick note - when converting Excel spreadsheets with many columns (like we have in this example), we should expect excess columns to run onto the subsequent page.

15 - output pdf page 1
15a - output pdf page 2

Conclusion

Yep – that’s all there is to it! We just made a clean, high-fidelity conversion from Excel to PDF in a few minutes with copy & paste code examples. We can implement this API anywhere we want with an extremely minimal footprint. It runs fast, requires zero maintenance, and abstracts unnecessary complexity away from our environment.

If you have any questions about the Cloudmersive Convert API or any other API services on our expansive API platform, please do not hesitate to review our product pages or contact a member of our team.

600 free API calls/month, with no expiration

Sign Up Now or Sign in with Google    Sign in with Microsoft

Questions? We'll be your guide.

Contact Sales