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
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:
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: