How to Convert Excel to PDF with Python

In modern workflow and data management, converting Excel spreadsheets to PDF is a ubiquitous need—whether for client reports, archival purposes, or cross-team collaboration. PDFs excel at preserving formatting, ensuring compatibility across devices, and preventing unintended edits, making them the gold standard for shareable documents. This comprehensive guide walks you through using Spire.XLS for Python to master Excel-to-PDF conversion, from basic one-click transforms to advanced, customized workflows.

Prerequisites & Installation

To get started, install the Spire.XLS library via Python’s pip package manager. Choose between the full-featured version or the free tier (with limitations):

Full Version

pip install Spire.XLS

Free Version (With Restrictions)

pip install Spire.XLS.Free

Note: The free version is ideal for personal projects or small-scale tasks.

Basic Excel-to-PDF Conversion (Entire Workbook)

Converting an entire Excel workbook to PDF is straightforward with Spire.XLS. The following code snippet handles loading the file, configuring page fitting, and exporting—all in just a few lines:

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

# Initialize a Workbook object
workbook = Workbook()

# Load the target Excel file (supports .xls, .xlsx, .xlsm, .xlsb)
workbook.LoadFromFile("sample.xlsx")

# Auto-fit worksheets to page dimensions (prevents content cutoff)
workbook.ConverterSetting.SheetFitToPage = True

# Convert and save as PDF
workbook.SaveToFile("output/EntireWorkbook.pdf", FileFormat.PDF)

# Clean up resources
workbook.Dispose()

print("Conversion completed successfully!")

Core Workflow Breakdown:

  1. Initialize: Create a Workbook instance to handle Excel file operations.
  2. Load: Use LoadFromFile() to import your Excel document (specify the full file path if it’s not in the working directory).
  3. Configure: Enable SheetFitToPage to ensure content adapts to PDF page size (avoids truncated data).
  4. Export: Save the workbook as a PDF with SaveToFile(), specifying the output path and format.
  5. Clean Up: Call Dispose() to free system resources (critical for batch processing).

Convert Specific Worksheets (Targeted Exports)

In most scenarios, you won’t need to convert an entire workbook—only select worksheets. Spire.XLS lets you target individual sheets by index (note: worksheet indices start at 0) or name:

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

# Load the Excel file
workbook = Workbook()
workbook.LoadFromFile("C:\Users\User\Documents\data.xlsx")

# Option 1: Target worksheet by index (e.g., 2nd worksheet = index 1)
target_sheet = workbook.Worksheets[1]

# Option 2: Target worksheet by name (more reliable for dynamic workbooks)
# target_sheet = workbook.Worksheets["Quarterly Report"]

# Configure page fitting
workbook.ConverterSetting.SheetFitToPage = True

# Export only the target worksheet to PDF
target_sheet.SaveToPdf("output/TargetWorksheet.pdf")

# Clean up
workbook.Dispose()

print("Specific worksheet converted successfully!")

Pro Tip: Use worksheet names instead of indices if your Excel file’s sheet order might change—this makes your code more robust.

Advanced PDF Customization

Spire.XLS offers granular control over PDF output, letting you tailor everything from page layout to print settings. Below are key customizations to elevate your converted PDFs:

1. Configure Page Layout

Customize page orientation, paper size, margins, and gridline visibility to match your use case (e.g., reports, invoices, or data sheets):

# Access PageSetup for layout customization
page_setup = sheet.PageSetup

# Set page orientation (Landscape for wide tables; Portrait for vertical content)
page_setup.Orientation = PageOrientationType.Landscape

# Specify paper size (A4 = standard; A3 for larger tables)
page_setup.PaperSize = PaperSizeType.PaperA4

# Set margins (adjust based on your printing needs)
page_setup.TopMargin = 0.5
page_setup.BottomMargin = 0.5
page_setup.LeftMargin = 0.75
page_setup.RightMargin = 0.75

# Toggle gridlines (enable for data-heavy sheets; disable for clean reports)
page_setup.IsPrintGridlines = True

2. Batch Convert Multiple Excel Files

For processing folders of Excel documents (e.g., monthly reports), use this scalable batch script. It automates file discovery, conversion, and error handling:

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

def batch_excel_to_pdf(input_dir, output_dir):
    """
    Batch convert all Excel files in a specified directory to PDF format

    Parameters:
        input_dir: Path to directory containing Excel files
        output_dir: Path to directory for saving PDF outputs
    """
    # Create output directory if it doesn't exist
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Filter Excel files by extension
    excel_extensions = ('.xls', '.xlsx', '.xlsm', '.xlsb')
    excel_files = [f for f in os.listdir(input_dir) 
                  if f.lower().endswith(excel_extensions)]

    if not excel_files:
        print(f"No Excel files found in directory: {input_dir}")
        return

    workbook = Workbook()

    try:
        for index, filename in enumerate(excel_files, 1):
            # Construct full file paths
            excel_path = os.path.join(input_dir, filename)
            pdf_filename = os.path.splitext(filename)[0] + '.pdf'
            pdf_path = os.path.join(output_dir, pdf_filename)

            print(f"Processing {index}/{len(excel_files)}: {filename}")

            # Load Excel file and convert to PDF
            workbook.LoadFromFile(excel_path)
            workbook.SaveToFile(pdf_path, FileFormat.PDF)

            # Reset workbook for next iteration
            workbook.Dispose()
            workbook = Workbook()

            print(f"Successfully saved: {pdf_filename}")

        print(f"Batch conversion complete! Processed {len(excel_files)} files total.")

    except Exception as e:
        print(f"Error during conversion: {str(e)}")

    finally:
        workbook.Dispose()

# Example usage
if __name__ == "__main__":
    input_directory = "Excel Files to Convert"  # Directory with source Excel files
    output_directory = "Converted PDF Files"    # Directory for output PDFs
    batch_excel_to_pdf(input_directory, output_directory)

Final Tips for Optimal Results

  1. Test with Sample Files: Always validate conversions with a small Excel file first to adjust settings (e.g., margins, orientation) before batch processing.
  2. Handle Large Files: For Excel files with multiple sheets or heavy data, disable SheetFitToPage if it causes delays—manually set column widths instead.
  3. Update the Library: Regularly run pip install --upgrade Spire.XLS to access new features and bug fixes.

Whether you’re a data analyst streamlining report sharing or a developer integrating conversion into a workflow, this guide equips you to handle every Excel-to-PDF scenario with confidence. With Spire.XLS for Python, what once took manual effort becomes an automated, reliable process—saving you time and ensuring consistent, professional results.

Leave a Reply