Convert Excel to PDF in Python: A Comprehensive Guide [Updated]

Alexander Stock
7 min readOct 8, 2023

--

Convert Excel to PDF.

Microsoft Excel is now a vital tool for data management and analysis. However, there are cases where sharing the original Excel file isn’t ideal due to reasons like formatting preservation, cross-device compatibility, or preventing unauthorized changes. That’s when converting Excel files to PDF format becomes necessary.

In this blog post, I will guide you through the process of converting Excel files to PDF using Python, demonstrating it with eight specific examples.

Install Dependency

This solution requires Spire.XLS for Python to be installed as the dependency, which is a Python library for reading, creating and manipulating Excel documents in a Python program. You can install Spire.XLS for Python by executing the following pip commands.

pip install Spire.XLS

Convert a Worksheet to PDF with Default Page Settings

In Microsoft Excel, page settings refer to the various parameters that determine how your worksheet will be printed or displayed when converted to a physical page or a digital format like PDF. These settings allow you to control aspects such as page orientation, paper size, margins, scaling, and more.

If you have already set the desired page settings for your worksheet, you can convert it to PDF using the Worksheet.SaveToPdf method provided by Spire.XLS.

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 specific worksheet (index starts at zero)
sheet = workbook.Worksheets[0]

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/WorksheetToPdf.pdf")

# Dispose resources
workbook.Dispose()

Convert a Worksheet to PDF with Custom Page Margins

By adjusting the margin settings, you can control the spacing between the content and the edges of the PDF page. This is useful when you want to place the content at the center of the page, or ensure that important data or elements are not too close to the page borders in the converted PDF.

Spire.XLS offers the PageSetup class to work with page settings including margins. This class provides properties such as TopMargin, BottomMargin, LeftMargin, and RightMargin, which are responsible for configuring the margins on each side of the page.

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")

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Get a specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set page margins
pageSetup.TopMargin = 0.1
pageSetup.BottomMargin = 0.1
pageSetup.LeftMargin = 0.1
pageSetup.RightMargin = 0.1

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/CostomizePageMargins.pdf")

# Dispose resources
workbook.Dispose()

Cut Unnecessary Blank Areas while Converting a Worksheet to PDF

When converting worksheets to PDFs, you can eliminate unnecessary blank areas. This ensures that the resulting PDF file accurately represents the content of the worksheet without any excessive empty space.

To cut unnecessary blank areas, you need first to set the page margins to small values or zero. This minimizes the empty space surrounding the content. Secondly, you’re required to set the Workbook.ConverterSetting.SheetFitToPage property to true, which makes sure that the PDF page size is adjusted to fit the content area of the worksheet.

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")

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Get a specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set page margins
pageSetup.TopMargin = 0.1
pageSetup.BottomMargin = 0.1
pageSetup.LeftMargin = 0.1
pageSetup.RightMargin = 0.1

# The PDF page size is adjusted to fit the content area
workbook.ConverterSetting.SheetFitToPage = True

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/RemoveBlankArea.pdf")

# Dispose resources
workbook.Dispose()
Convert a worksheet to a PDF file without unnecessary blank areas around the content.
Figure 1. Convert Worksheet to PDF without Unnecessary Blank Areas

Specify Page Size while Converting a Worksheet to PDF

Converting a worksheet to PDF with a standard page size like A4 ensures compatibility, consistency, and convenience when sharing, viewing, and printing the document. It promotes better document organization and aligns with widely accepted international standards.

Within the PageSetup class, you can utilize the PaperSize property to define the page size when converting a document to PDF. This property allows you to specify standard paper sizes such as A3, A4, B4, B5, or even a custom paper size according to your specific requirements.

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")

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Fit worksheet on one page while retaining paper size
workbook.ConverterSetting.SheetFitToPageRetainPaperSize = True

# Get a specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set the page size to A4
pageSetup.PaperSize = PaperSizeType.PaperA4

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/StandardPageSize.pdf")

# Dispose resources
workbook.Dispose()
Convert a worksheet to a PDF document with a standard paper size, like A4.
Figure 2. Convert Worksheet to PDF with a Standard Page Size

Preserve Gridlines while Converting a Worksheet to PDF

By preserving the gridlines during the conversion process, the resulting PDF file will maintain the visual representation of the grid structure from the original worksheet. This can be helpful for maintaining readability, data alignment, and overall visual consistency.

To enable the gridlines when converting worksheets to PDFs, set the PageSetup.IsPrintGridlines to True. The default value is False.

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 specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Preserve gridlines
pageSetup.IsPrintGridlines = True

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/PreserveGridlines.pdf")

# Dispose resources
workbook.Dispose()

Specify Page Orientation while Converting a Worksheet to PDF

When converting a worksheet to PDF, you may want to specify the page orientation to ensure that the content is presented in the desired layout. To do so, you set the PageSetup.Orientation to either Lanscape or Portrait.

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 specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set page orientation to Lanscape (or Portrait)
pageSetup.Orientation = PageOrientationType.Landscape

# Convert the worksheet to PDF file
sheet.SaveToPdf("output/PageOrientation.pdf")

# Dispose resources
workbook.Dispose()

Convert a Cell Range to PDF

By converting a specific cell range to PDF, you can create a focused and concise PDF document containing only the selected data. This is particularly useful when you need to share or distribute specific portions of a worksheet without including unnecessary information.

To specify a range of cells for conversion, you can assign a value, such as “A7:B11,” to the PageSetup.PrintArea property. This allows you to define the specific cell range that you want to include when converting the worksheet to PDF.

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")

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Get a specific worksheet (index starts at zero)
sheet = workbook.Worksheets[1]

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set print area
pageSetup.PrintArea = "A7:B11"

# Convert the range to PDF file
sheet.SaveToPdf("output/CellRangeToPdf.pdf")

# Dispose resources
workbook.Dispose()

Convert a Workbook to PDF

You can convert your workbook into a single PDF document with each sheet appearing on a separate page. This allows for easy navigation and viewing of each sheet’s content within the PDF file.

Using Spire.XLS, you have the capability to convert a workbook to a different file format, such as PDF, by employing the Workbook.SaveToFile method. Prior to the conversion process, you may need to customize the page settings for each sheet within the workbook and configure other specific options using the properties available under the ConverterSetting object.

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")

# Fit worksheet on one page
workbook.ConverterSetting.SheetFitToPage = True

# Fit worksheet on one page while retaining paper size
workbook.ConverterSetting.SheetFitToPageRetainPaperSize = True

# Iterate through the worksheets in the workbook
for sheet in workbook.Worksheets:

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set the page size to A4
pageSetup.PaperSize = PaperSizeType.PaperA4

# Convert the workbook to PDF
workbook.SaveToFile("output/WorkbookToPdf.pdf", FileFormat.PDF)

# Dispose resources
workbook.Dispose()

Get a Free Trial License

The code snippets mentioned above produce PDF documents that include a red evaluation message at the center of each page. If you wish to remove the watermark, you can get a 30-day trial license here.

Conclusion

In this blog post, I provide a comprehensive summary of different scenarios for converting Excel files to PDF. I cover topics such as adjusting page size, setting page orientation, managing page margins, specifying the conversion area, and more. I hope that this post proves to be valuable and informative for your needs.

Related Topics

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.