How to Convert Excel to PDF in Python



In this article, you will learn how to convert excel to PDF in Python.

How to Convert Excel to PDF

There are a few different libraries in Python that can be used to convert an Excel file to a PDF file. One popular library for this purpose is called “pyexcelerate”, which can be installed via pip:

pip install pyexcelerate

Here’s an example of how to use pyexcelerate to convert an Excel file to PDF:

import pyexcelerate as pe
from io import BytesIO
from reportlab.pdfgen import canvas

# Load the Excel file
workbook = pe.get_book(file_name='example.xlsx')

# Convert the workbook to a BytesIO object
excel_file = BytesIO()

# Create a new PDF file
pdf_file = BytesIO()
pdf_canvas = canvas.Canvas(pdf_file)

# Convert the Excel data to PDF
pdf_canvas.drawString(100, 750, "Excel to PDF Example")
pdf_canvas.drawString(100, 700, "Sheet 1")
for row in workbook['Sheet1']:
    for i, cell in enumerate(row):
        pdf_canvas.drawString(100 + i * 100, 650 - row.row_index * 25, str(cell))

# Save the PDF file

# Write the PDF file to disk
with open('example.pdf', 'wb') as f:

This code loads an Excel file called “example.xlsx”, converts it to a BytesIO object, creates a new PDF file in memory, and uses ReportLab to write the Excel data to the PDF file. Finally, it writes the PDF file to disk as “example.pdf”. You can customize this code to fit your specific needs, such as specifying a different input Excel file or output PDF file name, or modifying the PDF layout.