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:

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:

  1. Create a Workbook instance and load the source Excel file

  2. Configure converter settings such as SheetFitToPage to optimize page layout

  3. Save to PDF format by specifying FileFormat.PDF

  4. Dispose 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:

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:

Setting FitToPagesTall to 0 means "no limit," allowing the content to extend vertically across as many pages as necessary while maintaining the one-page-width constraint.

Converting Selected Range to PDF

Sometimes you only need to export a specific portion of a worksheet rather than the entire document. Spire.XLS allows you to copy a selected range to a new sheet and convert only that section to PDF, providing precise control over the exported content.

The following example demonstrates how to export a specific cell range:

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

# Define input and output file paths
inputFile = "./Demos/Data/ConversionSample1.xlsx"
outputFile = "SelectedRangeToPDF.pdf"

# Create a workbook and load the document
workbook = Workbook()
workbook.LoadFromFile(inputFile)

# Add a new worksheet to hold the selected range
workbook.Worksheets.Add("newsheet")

# Copy the desired range (A9:E15) from the first sheet to the new sheet
workbook.Worksheets[0].Range["A9:E15"].Copy(
    workbook.Worksheets[1].Range["A9:E15"], 
    False,  # Don't copy column widths
    True    # Copy formatting
)

# Auto-fit column widths for optimal display
workbook.Worksheets[1].Range["A9:E15"].AutoFitColumns()

# Save only the new sheet to PDF
workbook.Worksheets[1].SaveToPdf(outputFile)

# Release resources
workbook.Dispose()

This approach involves several steps:

  1. Load the original Excel file

  2. Create a new worksheet to hold the selected data

  3. Copy the specific range to the new sheet, preserving formatting

  4. Auto-fit column widths to ensure content displays properly

  5. Use SaveToPdf on the specific worksheet instead of the entire workbook

This technique is particularly useful for extracting summary tables, key metrics, or specific sections from larger spreadsheets.

Converting Each Worksheet to Separate PDFs

When working with workbooks containing multiple independent worksheets, you might want to generate separate PDF files for each sheet. This approach allows recipients to focus on relevant sections without navigating through unrelated content.

Here's how to iterate through all worksheets and save each as an individual PDF:

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

# Define input file path
inputFile = "./Demos/Data/EachWorksheetToDifferentPDFSample.xlsx"

# Load the document from disk
workbook = Workbook()
workbook.LoadFromFile(inputFile)

# Process each worksheet individually
for sheet in workbook.Worksheets:
    # Generate filename based on worksheet name
    FileName = sheet.Name + ".pdf"
    
    # Save the individual sheet to PDF
    sheet.SaveToPdf(FileName)

# Release resources
workbook.Dispose()

This loop-based approach offers several advantages:

This method is ideal for monthly reports where each worksheet represents a different department, region, or time period.

Practical Applications

Converting Excel to PDF finds numerous applications across different domains:

Financial Reporting

Finance teams regularly convert Excel-based financial statements to PDF for board meetings and investor presentations. The PDF format ensures that numbers, charts, and formatting remain intact during distribution. A batch processing function might look like this:

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

def ConvertExcelReportsToPDF(input_folder: str, output_folder: str):
    """Convert all Excel files in a folder to PDF with optimized settings"""
    
    # Create output folder if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # Process all Excel files in the input folder
    for filename in os.listdir(input_folder):
        if filename.endswith(".xlsx") or filename.endswith(".xls"):
            # Build full file paths
            input_path = os.path.join(input_folder, filename)
            output_filename = os.path.splitext(filename)[0] + ".pdf"
            output_path = os.path.join(output_folder, output_filename)
            
            # Convert the file with optimized settings
            workbook = Workbook()
            workbook.LoadFromFile(input_path)
            workbook.ConverterSetting.SheetFitToPage = True
            workbook.SaveToFile(output_path, FileFormat.PDF)
            workbook.Dispose()
            
            print(f"Converted: {filename} -> {output_filename}")

# Example usage
input_folder = "./Financial_Reports"
output_folder = "./PDF_Reports"
ConvertExcelReportsToPDF(input_folder, output_folder)

Invoice Generation

Businesses can automate invoice delivery by converting Excel invoice templates to PDF, ensuring professional presentation and preventing unauthorized modifications to billing amounts.

Data Export for Compliance

Regulated industries often require data exports in non-editable formats for audit trails. Converting Excel data to PDF/A format satisfies these regulatory requirements while maintaining data integrity.

Best Practices

To optimize your Excel to PDF conversions, keep the following recommendations in mind:

Conclusion

Converting Excel files to PDF using Python and Spire.XLS provides a powerful solution for generating professional, shareable documents from spreadsheet data. Whether you need simple format conversion, archival-compliant PDF/A output, selective range export, or per-worksheet PDF generation, these techniques enable efficient automation of document transformation tasks.

We have explored:

By mastering these techniques, you will be able to automate your Excel-to-PDF workflows effectively, improving productivity and ensuring consistent, professional-quality PDF output for all your business documentation needs.