Convert Excel to PDF with Python
Tags: python, excel, pdf, api
Published: 2023-11-16 06:42:25

Converting Excel files to PDF makes it easier to store tabular data. In addition, it ensures that the layout of an Excel document remains consistent across devices and operating systems when printing or sharing documents. It’s easy to save Excel documents directly to PDF format, but how to achieve this feature in Python? This article will share a simple way to convert Excel to PDF in Python using a third-party library - [Spire.XLS for Python](https://www.e-iceblue.com/Introduce/xls-for-python.html).

### Steps to Convert Excel to PDF in Python
① First, install the Python libraries via the following pip command.
```
pip install Spire.XLS
```
② After installation, import the required libraries
```python
from spire.xls import *
from spire.common import *
```
③ Load an Excel (.xls/ .xlsx) document, and then use the **Workbook.SaveToFile()** or **Worksheet.SaveToPdf()** to convert an Excel workbook or a specified worksheet to PDF format.

④ The **PageSetup** class can also be used to set the page (size, margins, etc.) during the conversion process.



**----Sample Code-----**

###✍ Convert Excel files to PDF format in Python (each worksheet is displayed as a single PDF page).
```python
from spire.xls import *
from spire.common import *
 
# Create a Workbook object
workbook = Workbook()
 
# Load an Excel document
workbook.LoadFromFile("Sample.xlsx")
 
# Set the worksheet fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True
 
# Convert Excel to PDF file
workbook.SaveToFile("ToPDF.pdf", FileFormat.PDF)
workbook.Dispose()
```

![Excel2PDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh59sdo8lxx21ggioayn.png)

### ✍ Convert each worksheet in Excel to a separate PDF file with Python.
```python
from spire.xls import *
from spire.common import *
 
# Create a Workbook object
workbook = Workbook()
 
# Load an Excel document
workbook.LoadFromFile("Sample.xlsx")
 
# Loop through all worksheets in the Excel workbook
for sheet in workbook.Worksheets:
 
# Save each worksheet as a separate PDF file
    FileName =  sheet.Name + ".pdf"
    sheet.SaveToPdf(FileName)
workbook.Dispose()
```

![ConvertExceltoPDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1wt9g843k66ufa6fihm.png)

### ✍ Convert a specified Excel worksheet to PDF format with Python.
```python
from spire.xls import *
from spire.common import *
 
# Create a Workbook object
workbook = Workbook()
 
# Load an Excel document
workbook.LoadFromFile("Sample.xlsx")
 
# Get the second 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 the page size
pageSetup.PaperSize = PaperSizeType.PaperA3
 
# Set the worksheet fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True
 
# Convert the worksheet to a PDF file
sheet.SaveToPdf("WorksheetToPDF.pdf")
workbook.Dispose()
```

----------

> - To remove the watermark: [https://www.e-iceblue.com/TemLicense.html](https://www.e-iceblue.com/TemLicense.html)
> - Learn more about the Python library: [https://www.e-iceblue.com/Tutorials/Python/Spire.XLS-for-Python/Program-Guide/Spire.XLS-for-Python-Program-Guide-Content.html](https://www.e-iceblue.com/Tutorials/Python/Spire.XLS-for-Python/Program-Guide/Spire.XLS-for-Python-Program-Guide-Content.html)