In many business workflows, Excel files are used to store and analyze data. However, when sharing reports or invoices, converting them to PDF ensures the content remains fixed and readable across devices. In this tutorial, you’ll learn how to convert Excel to PDF using Spire.XLS for Python , a professional library that makes Excel file manipulation and export simple and reliable.
What Is Spire.XLS for Python?
Spire.XLS for Python is a powerful library developed by E-iceblue for creating, reading, editing, and converting Excel files without needing Microsoft Excel installed. It supports XLS, XLSX, CSV, and ODS formats and allows exporting Excel workbooks to PDF, HTML, image, and other formats.
Some key advantages include:
- Works offline — no Microsoft Office dependency
- Supports all Excel versions
- Preserves original layout and formulas during conversion
- Offers flexible settings for page size, margins, and orientation
Step 1: Install Spire.XLS for Python
You can install Spire.XLS directly via pip :
pip install Spire.XLS
Once installed, you’re ready to work with Excel files programmatically.
Step 2: Load an Excel File
Before converting to PDF, load your Excel workbook into a Workbook object. Here’s a simple example:
from spire.xls import Workbook
# Create a Workbook instance
workbook = Workbook()
# Load an existing Excel file
workbook.LoadFromFile("SalesReport.xlsx")
This loads the Excel file into memory, allowing you to manipulate or directly export it.
Step 3: Convert Excel to PDF
After loading the workbook, you can export it to PDF with just one line of code:
# Save the workbook as a PDF file
workbook.SaveToFile("SalesReport.pdf", FileFormat.PDF)
That’s it! Your Excel file is now converted to a high-quality PDF. The layout, fonts, and colors remain consistent with the original Excel file.
Step 4: Convert Specific Worksheets to PDF
Sometimes, you may not need the entire workbook — just one or a few sheets. Spire.XLS lets you export individual worksheets selectively:
from spire.xls import Workbook, FileFormat
# Load the workbook
workbook = Workbook()
workbook.LoadFromFile("AnnualData.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Save the worksheet as a PDF
sheet.SaveToPdf("FirstSheet.pdf")
This approach is perfect for generating department-specific or summary reports.
Step 5: Customize PDF Settings
Spire.XLS provides flexible page setup options to control how the PDF looks — including orientation, paper size, and margins.
from spire.xls import Workbook, PageOrientationType, PaperSizeType, FileFormat
# Load workbook
workbook = Workbook()
workbook.LoadFromFile("FinancialReport.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Adjust page settings
page_setup = sheet.PageSetup
page_setup.Orientation = PageOrientationType.Landscape
page_setup.PaperSize = PaperSizeType.PaperA4
page_setup.TopMargin = 0.5
page_setup.BottomMargin = 0.5
# Save as PDF
workbook.SaveToFile("FinancialReport_A4_Landscape.pdf", FileFormat.PDF)
You can also control headers, footers, and scaling options for a clean and professional PDF output.
Step 6: Convert Multiple Excel Files in Batch
If you handle multiple Excel reports regularly, you can automate batch conversion easily:
import os
from spire.xls import Workbook, FileFormat
input_folder = "ExcelFiles"
output_folder = "PDFReports"
for filename in os.listdir(input_folder):
if filename.endswith(".xlsx"):
workbook = Workbook()
workbook.LoadFromFile(os.path.join(input_folder, filename))
pdf_name = os.path.splitext(filename)[0] + ".pdf"
workbook.SaveToFile(os.path.join(output_folder, pdf_name), FileFormat.PDF)
This script converts every Excel file in a folder into a corresponding PDF file — a huge time-saver for monthly reporting.
Troubleshooting Tips
- Fonts missing in PDF – Make sure the fonts used in the Excel file are installed on your system.
- Large file size – Use optimized images or avoid unnecessary formatting.
-
Incorrect page breaks – Adjust scaling or margins via
PageSetupproperties.
Why Use Spire.XLS Instead of Excel’s Built-in Save As PDF?
| Feature | Excel Save As PDF | Spire.XLS for Python |
|---|---|---|
| Works without Microsoft Office | ❌ | ✅ |
| Automation support | ❌ | ✅ |
| Batch conversion | ❌ | ✅ |
| Custom layout control | Limited | Full |
| Cross-platform | ❌ | ✅ |
For developers or IT teams who need to automate Excel-to-PDF workflows, Spire.XLS is clearly more flexible and reliable.
Conclusion
Converting Excel to PDF ensures your data is easily shareable and securely formatted. With Spire.XLS for Python , the entire process can be done with just a few lines of code — no manual steps, no Excel installation, and complete control over output settings. Whether you’re generating reports, invoices, or dashboards, Spire.XLS offers a fast, professional solution for high-quality PDF exports.
Top comments (0)