Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert Excel to PDF Using Python

  • Last Updated : 24 Feb, 2021

Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry.

In this article, we will learn how to convert an Excel File to PDF File Using Python

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Here we will use the win32com module for file conversion.



What is win32com?

Python extensions for Microsoft Windows Provide access to much of the Win32 API, the ability to create and use COM objects, and the Pythonwin environment.

pip install pywin32

Approach:

  • First, we will create a COM Object Using Dispatch() method.
  • Then we will read the Excel file pass “Excel.Application” inside the Dispatch method
  • Pass Excel file path
  • Then we will convert it into PDF using the ExportAsFixedFormat() method

ExportAsFixedFormat():- The ExportAsFixedFormat method is used to publish a workbook in either PDF or XPS format.

Syntax:

ExportAsFixedFormat (Type, FileName, Quality, IncludeDocProperties, IgnorePrintAreas, From, To, 

OpenAfterPublish, FixedFormatExtClassPtr)

Excel File click here

Input:

EXCEL FILE

Below is the Implementation:

Python3




# Import Module
from win32com import client
  
# Open Microsoft Excel
excel = client.Dispatch("Excel.Application")
  
# Read Excel File
sheets = excel.Workbooks.Open('Excel File Path')
work_sheets = sheets.Worksheets[0]
  
# Convert into PDF File
work_sheets.ExportAsFixedFormat(0, 'PDF File Path')

Output:

PDF FILE

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!