Python - How to Convert Excel XLS or XLSX to PDF

Alice Yang
4 min readJun 6, 2023

--

Convert Excel to PDF with Python

Converting Excel files to PDF can be useful in a variety of situations. Here are some common reasons why we might need to convert an Excel file to a PDF:

  • Sharing and distribution: If we want to share an Excel file with others, but don’t want them to be able to edit or modify the contents, converting it to a PDF is a good option. PDFs are read-only documents that preserve the formatting and layout of the original Excel file, making them ideal for sharing and distributing reports, invoices, and other types of documents.
  • Archiving: PDF files, specifically, PDF/A files, are widely used for long-term archiving of important documents because they are platform-independent and do not require any special software to view. Converting an Excel file to a PDF/A ensures that the document will be easily accessible and readable in the future, even if the original software used to create it becomes obsolete.
  • Printing: If we need to print an Excel file, but want to ensure that it prints correctly and looks the way we want it to, converting it to a PDF can help. PDFs are designed for high-quality printing and offer a range of options for scaling, page orientation, and other printing-related settings.
  • Legal and regulatory compliance: In some industries, such as finance and healthcare, there may be strict regulations governing the storage and sharing of sensitive data. Converting an Excel file to a PDF can help ensure compliance with these regulations by providing a secure and tamper-proof format for storing and sharing data.

In this article, we will explore how to programmatically convert Excel XLS or XLSX files to PDF using Python. The following topics will be discussed:

Python Library to Convert Excel to PDF

To convert Excel files to PDF in Python, this article uses a Python Excel library: Spire.XLS for Python.

Spire.XLS for Python is a multifunctional and easy-to-use library for creating, reading, editing, and converting Excel files within Python applications. With this library, you can easily work with a lot of spreadsheet formats, such as XLS, XLSX, XLSB, XLSM, and ODS. Moreover, you are also able to render Excel files to other types of file formats, such as PDF, HTML, CSV, Text, Image, XML, SVG, ODS, PostScript, and XPS.

You can easily install Spire.XLS for Python from pypi by running the following commands in your terminal:

pip install Spire.Xls

For more detailed information about the installation, you can check this official documentation: How to Install Spire.XLS for Python in VS Code.

Convert an Excel XLS or XLSX File to PDF in Python

Converting an Excel file to a PDF document preserves the formatting and layout of the original file while making it easier to share, distribute, print, or archive.

The following example demonstrates how to convert an Excel file to a PDF file using Spire.XLS for Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel XLS or XLSX file
workbook.LoadFromFile("Sample.xlsx")

#Fit each worksheet to one page
workbook.ConverterSetting.SheetFitToPage = True
#convert the Excel file to PDF format
workbook.SaveToFile("ExcelToPDF.pdf", FileFormat.PDF)
workbook.Dispose()

Convert the Worksheets in an Excel File to Separate PDF Files in Python

Converting each worksheet from an Excel file to a separate PDF is required when we need to distribute each worksheet separately.

The following example demonstrates how to convert the worksheets in an Excel file to separate PDF files using Spire.XLS for Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Iterate through the worksheets in the file
for sheet in workbook.Worksheets:
FileName = sheet.Name + ".pdf"
#Save each sheet to a separate PDF
sheet.SaveToPdf(FileName)
workbook.Dispose()

Convert a Specific Cell Range in an Excel Worksheet to PDF in Python

Converting a specific cell range in an Excel worksheet to PDF is useful when we need to share only a portion of the data in the worksheet.

The following example demonstrates how to convert a specific cell range in an Excel worksheet to PDF using Spire.XLS for Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Get the first worksheet
worksheet = workbook.Worksheets[0]

#Get the reference of the PageSetup of the worksheet
pageSetup = worksheet.PageSetup
#Specify the cells range of the print area
pageSetup.PrintArea = "A1:C5"

#Save the worksheet to PDF
worksheet.SaveToPdf("CellRangeToPDF.pdf")
workbook.Dispose()

Convert an Excel File to PDF/A in Python

PDF/A is a specialized version of PDF designed for long-term archiving of electronic documents. Converting an Excel file to PDF/A ensures that the resulting document is compliant with industry standards for long-term preservation, including requirements for font embedding, metadata, and digital signatures.

With Spire.XLS for Python, you can convert an Excel file to various PDF/A formats, including:

  • PDF/A-1a
  • PDF/A-1b
  • PDF/A-2a
  • PDF/A-2b
  • PDF/A-3a
  • PDF/A-3b

The following example demonstrates how to convert an Excel file to PDF/A-1a using Spire.XLS for Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Convert the Excel file to PDF/A-1a
workbook.ConverterSetting.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1A
workbook.SaveToFile("ExcelToPDFA.pdf", FileFormat.PDF)
workbook.Dispose()

Conclusion

In this article, we have demonstrated various ways to convert Excel files to PDF in Python with Spire.XLS for Python library. We hope it’s helpful to you.

Related Topics

Easily Create or Edit Excel Files with Python

Read Data from Excel Files in Python — A Comprehensive Guide

Python — How to Export Data from Database to Excel (Step by Step Guide)

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.