Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
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:
EXCEL FILE
Below is the Implementation:
Python3
from
win32com
import
client
excel
=
client.Dispatch(
"Excel.Application"
)
sheets
=
excel.Workbooks.
Open
(
'Excel File Path'
)
work_sheets
=
sheets.Worksheets[
0
]
work_sheets.ExportAsFixedFormat(
0
,
'PDF File Path'
)
Output:
PDF FILE
Like Article
Save Article
In this post, you’ll learn how to convert Excel files to PDFs in your Python application using PSPDFKit’s XLSX to PDF Python API. With our API, you can convert up to 100 PDF files per month for free. All you need to do is create a free account to get access to your API key.
PSPDFKit API
Document conversion is just one of our 30+ PDF API tools. You can combine our conversion tool with other tools to create complex document processing workflows. You’ll be able to convert various file formats into PDFs and then:
-
Merge several resulting PDFs into one
-
OCR, watermark, or flatten PDFs
-
Remove or duplicate specific PDF pages
Once you create your account, you’ll be able to access all our PDF API tools.
Step 1 — Creating a Free Account on PSPDFKit
Go to our website, where you’ll see the page below, prompting you to create your free account.
Once you’ve created your account, you’ll be welcomed by the page below, which shows an overview of your plan details.
As you can see in the bottom-left corner, you’ll start with 100 documents to process, and you’ll be able to access all our PDF API tools.
Step 2 — Obtaining the API Key
After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following page, which is an overview of your keys:
Copy the Live API Key, because you’ll need this for the Excel to PDF API.
Step 3 — Setting Up Folders and Files
Now, create a folder called excel_to_pdf
and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside excel_to_pdf
and name them input_documents
and processed_documents
.
Now, copy your Excel file to the input_documents
folder and rename it to document.xlsx
. You can use our demo document as an example.
Then, in the root folder, excel_to_pdf
, create a file called processor.py
. This is the file where you’ll keep your code.
Your folder structure will look like this:
excel_to_pdf ├── input_documents | └── document.xlsx ├── processed_documents └── processor.py
Step 4 — Writing the Code
Open the processor.py
file and paste the code below into it:
import requests import json instructions = { 'parts': [ { 'file': 'document' } ] } response = requests.request( 'POST', 'https://api.pspdfkit.com/build', headers = { 'Authorization': 'Bearer YOUR API KEY HERE' }, files = { 'document': open('input_documents/document.xlsx', 'rb') }, data = { 'instructions': json.dumps(instructions) }, stream = True ) if response.ok: with open('processed_documents/result.pdf', 'wb') as fd: for chunk in response.iter_content(chunk_size=8096): fd.write(chunk) else: print(response.text) exit()
ℹ️ Note: Make sure to replace
YOUR_API_KEY_HERE
with your API key.
Code Explanation
In the code above, you first import the requests
and json
dependencies. After that, you create the instructions for the API call.
You then use the requests
module to make the API call, and once it succeeds, you store the result in the processed_documents
folder.
Output
To execute the code, use the command below:
Once the code has been executed, you’ll see a new processed file under the processed_documents
folder called result.pdf
.
The folder structure will look like this:
excel_to_pdf ├── input_documents | └── document.xlsx ├── processed_documents | └── result.pdf └── processor.py
Final Words
In this post, you learned how to easily and seamlessly convert Excel files to PDF documents for your Python application using our Excel to PDF Python API.
You can integrate these functions into your existing applications. With the same API token, you can also perform other operations, such as merging several documents into a single PDF, adding watermarks, and more. To get started with a free trial, sign up here.
Describes how to convert an Excel sheet to PDF using Python.
There seem to be many libraries for manipulating Excel files in Python, but this time I will use a module called win32com.
win32com can handle Windows COM objects. Just like using COM objects in Excel VBA.
Environment
- Windows 10
- Microsoft Excel 2016
- Python 3.6.2
Install
win32com is not included by default, so install it.
Now you can do import win32com
.
Convert Excel file to PDF
I prepared the pre-conversion Excel file (I just brought an Excel template).
One sheet is a calendar for one month, and there are 12 sheets (one year).
Convert this to a single PDF file.
Excel file
To convert an Excel file to PDF, simply operate Excel from win32com, open the file and save it as PDF.
The following is a sample program.
import win32com.client
from pywintypes import com_error
# Path to original excel file
WB_PATH = r'C:hogefugaYearCalendar.xlsx'
# PDF path when saving
PATH_TO_PDF = r'C:hogefugaYearCalendar.pdf'
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
try:
print('Start conversion to PDF')
# Open
wb = excel.Workbooks.Open(WB_PATH)
# Specify the sheet you want to save by index. 1 is the first (leftmost) sheet.
ws_index_list = [1,2,3,4,5,6,7,8,9,10,11,12]
wb.WorkSheets(ws_index_list).Select()
# Save
wb.ActiveSheet.ExportAsFixedFormat(0, PATH_TO_PDF)
except com_error as e:
print('failed.')
else:
print('Succeeded.')
finally:
wb.Close()
excel.Quit()
All 12 sheets are specified and exported to a PDF file.
The output PDF is as follows.
Output PDF file
I was able to create PDF more easily than I expected.
It is interesting that there are many other possibilities.
Reference
See also
- Worksheet.ExportAsFixedFormat method (Excel)
- Excel VBA reference
Содержание
- Convert Excel XLS XLSX to PDF in Python
- Python Excel to PDF Converter — Free Download#
- How Convert Excel to PDF in Python#
- Save Excel XLS as PDF in Python#
- Excel File#
- Converted PDF#
- Python XLSX to PDF — Advanced Conversion#
- Python Excel to PDF Converter — Get a Free License#
- Conclusion#
- Excel to PDF Python Converter — Explore More#
- See Also#
- Convert Excel to PDF Using Python
- What is win32com?
- How to convert Excel file to PDF with Python
- Environment
- Install
- Convert Excel file to PDF
- Reference
- Related Posts
- [Python] Replace text in Excel cells
- How to open Excel file with password in PowerShell
- Replace string in Excel with PowerShell
- Make Python & Selenium program executable (.exe) (How to include webdriver in .exe)
- [PyInstaller]Make Python an executable file
- [PyInstaller] Create multiple exe’s in one folder
- Hide the console in Python Selenium
- Convert Excel Spreadsheets to PDF using Python
- Python SDK to Convert Documents to PDF#
- Convert Excel Spreadsheets to PDF in Python#
- Convert XLS, XLSX to PDF and Show Gridlines in Python#
- Convert Excel Spreadsheets to PDF with Specific Range in Python#
- Customizations while Converting Spreadsheet to PDF#
- Quickly extract Table from PDF to Excel with Python
- PDF to Excel (one table only)
- PDF containing several tables
Convert Excel XLS XLSX to PDF in Python
Nowadays, PDF has become a standard file format for exchanging documents. Various popular document formats are converted to PDF before they are shared over the Internet. Excel to PDF is one of the popular conversion scenarios in which worksheets are converted to read-only PDF pages. In accordance with that, this article covers how to convert Excel XLSX or XLS files to PDF using Python.
Python Excel to PDF Converter — Free Download#
For Excel to PDF conversion, we’ll use Aspose.Cells for Python. It is a powerful spreadsheet manipulation API that lets you create, process, and convert Excel files from within your Python applications. You can install the API using the following pip command.
How Convert Excel to PDF in Python#
The conversion of Excel files to PDF is as simple as pie using Aspose.Cells for Python. The following steps are required to save an Excel file in PDF format.
- Load the Excel file from desired location.
- Save it as PDF to the desired location.
And that’s it. Let’s have a look at how to transform these steps into code and convert an Excel XLS/XLSX file to PDF in Python.
Save Excel XLS as PDF in Python#
The following are the steps to convert an Excel XLSX file to PDF using Python.
- Load the Excel XLSX file using the Workbook class.
- Convert XLSX to PDF using Workbook.save(fileName, SaveFormat.PDF) method.
The following code sample shows how to convert an Excel XSLX file to PDF.
Excel File#
Converted PDF#
Python XLSX to PDF — Advanced Conversion#
Aspose.Cells for Python also allows you to customize the XLSX to PDF conversion using different options. For example, you can set PDF compliance, compression, gridlines style, number of pages per sheet, etc. for the converted PDF file. The PdfSaveOptions class is used to set these options.
The following are the steps to use advanced options in XLSX to PDF conversion in Python.
- Load the Excel XLSX file using the Workbook class.
- Create an instance of PdfSaveOptions class.
- Use PdfSaveOptions object to set options, e.g. set PDF compliance using PdfOptions.setCompliance(PdfCompliance) method.
- Save XLSX as PDF using Workbook.save(fileName, saveOptions) method.
The following code sample shows how to use advanced options in Excel XLSX to PDF conversion.
Python Excel to PDF Converter — Get a Free License#
You can request a temporary license in order to use the API without evaluation limitations.
Conclusion#
In this article, you have learned how to convert Excel XLSX or XLS to PDF in Python. Furthermore, the advanced options to control the Python Excel to PDF conversion are also discussed with the help of the code sample.
Excel to PDF Python Converter — Explore More#
You can explore more about Aspose.Cells for Python via Java using the documentation. In case you would have any queries, contact us via our forum.
See Also#
Info: Aspose developed a free online web app that allows you to view PDFs online, another that allows you to convert PDFs to video, and one that allows you to edit PDFs online.
Источник
Convert Excel to PDF Using Python
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.
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.
ExportAsFixedFormat (Type, FileName, Quality, IncludeDocProperties, IgnorePrintAreas, From, To,
Input:
EXCEL FILE
Below is the Implementation:
Источник
How to convert Excel file to PDF with Python
Published: Aug. 10, 2019
Describes how to convert an Excel sheet to PDF using Python.
There seem to be many libraries for manipulating Excel files in Python, but this time I will use a module called win32com.
win32com can handle Windows COM objects. Just like using COM objects in Excel VBA.
Environment
- Windows 10
- Microsoft Excel 2016
- Python 3.6.2
Install
win32com is not included by default, so install it.
Now you can do import win32com .
Convert Excel file to PDF
I prepared the pre-conversion Excel file (I just brought an Excel template).
One sheet is a calendar for one month, and there are 12 sheets (one year).
Convert this to a single PDF file.
To convert an Excel file to PDF, simply operate Excel from win32com, open the file and save it as PDF.
The following is a sample program.
All 12 sheets are specified and exported to a PDF file.
The output PDF is as follows.
Output PDF file
I was able to create PDF more easily than I expected.
It is interesting that there are many other possibilities.
Reference
[Python] Replace text in Excel cells
Replace strings in Excel cells with Python. It can be used …
How to open Excel file with password in PowerShell
Open an Excel workbook with password in PowerShell. When …
Replace string in Excel with PowerShell
Replace strings in cells in Excel using PowerShell. Imagine …
Make Python & Selenium program executable (.exe) (How to include webdriver in .exe)
Make a program created with Python & Selenium into an …
[PyInstaller]Make Python an executable file
PyInstaller can be used to convert a program created with …
[PyInstaller] Create multiple exe’s in one folder
PyInstaller makes it easy to create an exe, but I …
Hide the console in Python Selenium
Running Python Selenium causes the ChromeDriver console …
Источник
Convert Excel Spreadsheets to PDF using Python
This article will guide you to convert Excel Spreadsheets (XLS, XLSX) to PDF format in Python. Excel spreadsheets are widely used to maintain invoices, ledgers, inventory, accounts, and other reports. On the other hand, PDF is also one of the most commonly used formats and famous for its portability. Conversion among these two formats is widely required by users and programmers as well.
Python was developed in the 1990s and is now continuing to be one of the best and most popular languages, every developer should learn in 2020[1]. Let’s move ahead with your Excel files on Cloud storage get converted to PDF using Python.
Python SDK to Convert Documents to PDF#
I will be using the Python SDK of GroupDocs.Conversion Cloud API for conversions in this article, so get your APP KEY and APP SID from the dashboard before your start following the steps and available code examples.
Convert Excel Spreadsheets to PDF in Python#
Below are the simple steps to convert any XLS, XLSX spreadsheet to PDF using Python:
- Upload the Spreadsheet on the Cloud.
- Convert the uploaded Spreadsheet.
- Download the converted PDF document.
Python code is shown below to give you a better idea about how simple it is:
- Set the Convert Settings (File path and target format).
- Set the load options using SpreadsheetLoadOptions.
- Call the convert_document function to convert.
- Download the converted PDF from the provided URL.
Convert XLS, XLSX to PDF and Show Gridlines in Python#
Showing spreadsheet gridlines in a PDF is not always needed but required some times. So here is a simple option that allows showing gridlines in a PDF when needed.
Convert Excel Spreadsheets to PDF with Specific Range in Python#
It is not necessary to convert the whole Excel Workbook or Spreadsheet all the time. We can also convert the required portion of the spreadsheet by specifying the range in the following manner.
Customizations while Converting Spreadsheet to PDF#
There are many conversion customizations while converting the spreadsheets to PDF, like:
- Show Spreadsheet Gridlines in PDF
- loadOptions.show_grid_lines = True
- Hide Spreadsheet Comments in PDF
- loadOptions.hide_comments = True
- Skip Spreadsheet Empty Rows and Columns
- loadOptions.skip_empty_rows_and_columns = True
- Change Spreadsheet Font in PDF
- loadOptions.default_font = “Helvetica” loadOptions.font_substitutes =
- Convert the Specific Range of Spreadsheets to PDF
- loadOptions.convert_range = “1:35”
- Show Hidden Sheets of Excel in PDF
- loadOptions.show_hidden_sheets = True
The best and easiest way to try out all the above options is to run the available examples on GitHub repository.
You can learn more about the API from the documentation or Let’s talk more @ Free Support Forum.
Источник
In this article we will see how to quickly extract a table from a PDF to Excel.
For this tutorial you will need two Python libraries :
To install them, go to your terminal/shell and type these lines of code:
If you use Google Colab, you can install these libraries directly on it. You just have to add an exclamation mark “!” in front of it, like this:
PDF to Excel (one table only)
First we load the libraries into our text editor :
Then, we will read the pdf with the read_pdf() function of the tabula library.
This function automatically detects the tables in a pdf and converts them into DataFrames. Ideal for converting them into Excel files!
We can then check that the table has the expected shape.
Then convert it to an Excel file !
The entire code :
THE PANE METHOD FOR DEEP LEARNING!
Get your 7 DAYS FREE TRAINING to learn how to create your first ARTIFICIAL INTELLIGENCE!
For the next 7 days I will show you how to use Neural Networks.
You will learn what Deep Learning is with concrete examples that will stick in your head.
BEWARE, this email series is not for everyone. If you are the kind of person who likes theoretical and academic courses, you can skip it.
But if you want to learn the PANE method to do Deep Learning, click here :
PDF containing several tables
We load the libraries in our text editor :
Then, we will read the pdf with the read_pdf() function of the tabula library.
This function automatically detects the tables in a pdf and converts them into DataFrames. Ideal to convert them then in Excel file !
Here, the variable df will be in fact a list of DataFrame. The first element corresponds to the first table, the second to the second table, etc.
To save these tables separately, you will have to use a for loop that will save each table in an Excel file.
The entire code :
THE PANE METHOD FOR DEEP LEARNING!
Get your 7 DAYS FREE TRAINING to learn how to create your first ARTIFICIAL INTELLIGENCE!
For the next 7 days I will show you how to use Neural Networks.
You will learn what Deep Learning is with concrete examples that will stick in your head.
BEWARE, this email series is not for everyone. If you are the kind of person who likes theoretical and academic courses, you can skip it.
But if you want to learn the PANE method to do Deep Learning, click here :
Tom Keldenich
Data Engineer & passionate about Artificial Intelligence !
Founder of the website Inside Machine Learning
Источник