Python (355)
Converting Excel files to PDF format can be a useful way to share and distribute spreadsheets, especially if you want to ensure that the formatting and layout of the file remains consistent across different devices and software. In addition, PDFs often appear more polished and professional than Excel files, making them a popular choice for official reports, presentations, and other business documents. In this article, you will learn how to convert Excel to PDF in Python using Spire.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Convert a Whole Excel Document to PDF in Python
The Workbook.SaveToFile() method is used to convert a complete Excel document into a single PDF file. Once the conversion is done, each worksheet will appear as a separate page in the resultant PDF file. To control the conversion settings, use the Workbook.ConverterSetting property.
The following are the detailed steps to convert an Excel document to a PDF file in Python.
- Create a Workbook object.
- Load an Excel document using Workbook.LoadFromFile() method.
- Set the margins of every worksheet through Worksheet.PageSetup property, which will later become the blank edges of the generated PDF.
- Set the Excel to PDF conversion options through the properties under Workbook.ConverterSetting object.
- Convert the whole Excel document to PDF using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")
# Iterate through the worksheets in the workbook
for sheet in workbook.Worksheets:
# Get the PageSetup object
pageSetup = sheet.PageSetup
# Set page margins
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3
# Set worksheet to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True
# Convert to PDF file
workbook.SaveToFile("output/ToPdf.pdf", FileFormat.PDF)
workbook.Dispose()

Convert a Particular Worksheet to PDF in Python
The Worksheet.SaveToPdf() method is used to convert a specific worksheet into a PDF document. The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel document using Workbook.LoadFromFile() method.
- Get a particular worksheet through Workbook.Worksheets[] property.
- Set the margins of the worksheet through Worksheet.PageSetup property, which will later become the blank edges of the generated PDF.
- Set the Excel to PDF conversion options through the properties under Workbook.ConverterSetting object.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.xlsx")
# Get a particular worksheet
sheet = workbook.Worksheets[1]
# Get the PageSetup object
pageSetup = sheet.PageSetup
# Set page margins
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3
# Set worksheet to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True
# Convert the worksheet to PDF file
sheet.SaveToPdf("output/WorksheetToPdf.pdf")
workbook.Dispose()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.PDF for Python is a professional PDF development component that enables developers to create, read, edit, convert, and save PDF files in Python programs without depending on any external applications or libraries. This Python PDF class library provides developers with various functions to create PDF files from scratch or process existing PDF documents completely through Python programs.
Spire.PDF for Python supports various PDF processing features including security settings, extracting text/image from PDF, merging/splitting PDF, adding page numbers to PDF, drawing text/image/shape/barcode to PDF, creating/filling in form fields, adding and deleting PDF layers, cropping pages, copying pages, overlaying PDF, creating actions in PDF, inserting text/image watermarks to PDF, adding/deleting/extracting annotations, adding/updating/deleting PDF bookmarks, adding tables to PDF, compressing PDF documents etc.
Spire.Doc for Python is a professional Word Python API specifically designed for developers to create, read, write, convert and compare Word documents with fast and high-quality performance.
As an independent Word Python API, Spire.Doc for Python doesn't need Microsoft Word to be installed on neither the development nor target systems. However, it can incorporate Microsoft Word document creation capabilities into any developers' Python applications.
Spire.XLS for Python is a professional Excel development component that can be used to create, read, write and convert Excel files on any Python platform. Spire.XLS for Python offers object model Excel API for speeding up Excel programming in Python platform - create new Excel documents from template, edit existing Excel documents and convert Excel files.
Spire.XLS for Python enjoys good reputation in both enterprise and individual customers. These customer types include Banks, Data processing houses, Educational institutions, Government organizations, Insurance firms, Legal institutions, Postal/cargo services and etc.
Spire.XLS for Python is a Python library for reading, creating, editing and converting Excel (.xls & .xlsx) files in any Python application. This article shows you how to install Spire.XLS for Python on Windows.
Step 1
Download the latest version of Python and install it on your computer. If you have already installed it, skip to step 2.

Step 2
Click 'Extensions' in VS Code, search for 'Python' and then install it.

Step 3
Click 'Explorer' - 'NO FOLRDER OPENED' - 'Open Folder'.

Choose an existing folder as the workspace, or you can create a new folder and then select it.

Add a .py file to the folder you've just added (Python folder in this case), and name it whatever you like.

Step 4
Click 'Terminal' and then 'New Terminal'.

Input the following command to install Spire.XLS for Python and plum-dispatch v1.7.4.
pip install Spire.XLS

Alternatively, you can download Spire.XLS for Python from our website, and unzip it to get the .whl file from the 'lib' folder.

Then, install Spire.XLS for Python and plum-dispatch v1.7.4 by running the following commands.
pip install E:\Library\Python\spire.xls.python_13.5.0\lib\Spire.Xls-13.5.0-py3-none-any.whl

Step 5
Add the following code snippet to the 'HelloWorld.py' file.
- Python
from spire.xls.common import *
from spire.xls import *
workbook = Workbook()
sheet = workbook.Worksheets[0]
sheet.Range["A1"].Text = "Hello World"
sheet.Range["A1"].AutoFitColumns()
workbook.SaveToFile("HelloWorld.xlsx", ExcelVersion.Version2010)
workbook.Dispose()

Once you run the Python file, you'll see the result Excel document in the 'EXPORER' panel.





