Converting Excel spreadsheets to PDF format is an essential task for sharing data-driven reports, financial statements, and analytical summaries. PDF documents ensure that your Excel data maintains its formatting, layout, and visual integrity across different devices and platforms, making them ideal for distribution and archival purposes.
This article demonstrates how to use Python and the Spire.XLS library to convert Excel files (.xlsx) to PDF format with various customization options, from basic conversion to advanced features like page fitting and selective range export.
Why Convert Excel to PDF?
Converting Excel spreadsheets to PDF addresses several practical needs:
Consistent presentation: PDFs preserve cell formatting, charts, and layout exactly as designed
Professional delivery: PDF reports look polished and are suitable for client presentations
Print optimization: PDFs provide better control over page breaks and print layouts
Data protection: PDF format prevents accidental modification of formulas and data
Universal compatibility: Recipients don't need Excel installed to view the content
Automating this conversion through Python enables efficient processing of multiple reports and ensures consistent output quality.
Environment Setup
To get started, you need to install the Spire.XLS for Python library. This can be done easily using pip:
pip install Spire.XLS
Once installed, you can import the library in your Python scripts and access all the conversion features it provides.
Basic Excel to PDF Conversion
Simple Conversion with SaveToFile
The most straightforward approach to converting an Excel file to PDF is by using the SaveToFile method of the Workbook class. This method handles the entire conversion process, transforming all worksheets in the workbook into a single PDF document.
Here's a simple example demonstrating how to load an Excel file and save it as a PDF:
from spire.xls import *
from spire.xls.common import *
# Define input and output file paths
inputFile = "./Demos/Data/ToPDF.xlsx"
outputFile = "ToPDF.pdf"
# Create a workbook object
workbook = Workbook()
# Load the Excel document from disk
workbook.LoadFromFile(inputFile)
# Enable sheet fitting to page for better readability
workbook.ConverterSetting.SheetFitToPage = True
# Convert to PDF file
workbook.SaveToFile(outputFile, FileFormat.PDF)
# Release resources
workbook.Dispose()
This process follows several key steps:
Create a
Workbookinstance and load the source Excel fileConfigure converter settings such as
SheetFitToPageto optimize page layoutSave to PDF format by specifying
FileFormat.PDFDispose of the workbook to free system resources
The SheetFitToPage setting is particularly important as it ensures that each worksheet fits neatly on a single page, preventing awkward page breaks that could disrupt data readability.
Advanced Conversion Options
Converting to PDF/A for Archival
For documents that need to be preserved long-term, PDF/A format provides an ISO-standardized version of PDF designed specifically for archiving. This format embeds all necessary fonts and color profiles, ensuring the document can be reproduced exactly in the future.
The following example shows how to convert an Excel file to PDF/A-1B format:
from spire.xls import *
from spire.xls.common import *
# Define input and output file paths
inputFile = "./Demos/Data/ToPDF_A1BExample.xlsx"
outputFile = "ToPDFA1B.pdf"
# Create a workbook object
workbook = Workbook()
# Load the Excel file
workbook.LoadFromFile(inputFile)
# Set PDF conformance level to PDF/A-1B for archival compliance
workbook.ConverterSetting.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1B
# Convert to PDF with archival standard
workbook.SaveToFile(outputFile, FileFormat.PDF)
# Release resources
workbook.Dispose()
By setting PdfConformanceLevel to Pdf_A1B, the converter produces a PDF that meets strict archival requirements:
Self-contained: All fonts and resources are embedded within the file
Long-term preservation: Guaranteed to be readable decades into the future
Regulatory compliance: Meets legal and governmental archiving standards
This format is ideal for financial records, legal documents, and any data that must be preserved unchanged for extended periods.
Fitting Content to Page Width
When dealing with wide spreadsheets, controlling how content flows across pages is crucial for readability. The FitToPagesWide and FitToPagesTall properties allow you to specify exactly how many pages the content should span horizontally and vertically.
Here's how to fit all worksheets to one page width while allowing unlimited height:
from spire.xls import *
from spire.xls.common import *
# Define input and output file paths
inputFile = "./Demos/Data/SampleB_2.xlsx"
outputFile = "FitWidthWhenConvertToPDF.pdf"
# Load the document from disk
workbook = Workbook()
workbook.LoadFromFile(inputFile)
# Configure page setup for each worksheet
for sheet in workbook.Worksheets:
# Set FitToPagesTall to 0 for unlimited vertical pages
sheet.PageSetup.FitToPagesTall = 0
# Fit content to one page width
sheet.PageSetup.FitToPagesWide = 1
# Save to PDF with configured page settings
workbook.SaveToFile(outputFile, FileFormat.PDF)
# Release resources
workbook.Dispose()
This configuration offers several benefits:
Romney JovitaPosted Apr 16, 2026, 7:00 AM
Good job! thks