Table of Contents
Install with Pip
pip install Spire.XLS
Related Links
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 VS Code 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 in VS Code
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.