Open In App

How to convert CSV File to PDF File using Python?

Last Updated : 16 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps :

  1. Firstly, We convert our CSV file to HTML using the Pandas
  2. In the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format.

Approach:

1. Converting CSV file to HTML using Pandas Framework.

Pandas is a fast, powerful, flexible, and easy-to-use open-source data analysis and manipulation tool, built on top of the Python programming language.

CSV File Used:

For this section of tutorial we will be using :

  1. pandas.read_csv(): read_csv is an important pandas function to read CSV files and do operations on it.We will be using it to read our input CSV file.
  2. .to_html(): With help of DataFrame.to_html() method, we can get the html format of a dataframe by using DataFrame.to_html() method.This function takes in a CSV file as input, converts it, and saves it locally in HTML file format.

Syntax for converting CSV to HTML using Pandas :

import pandas as pd 

CSV = pd.read_csv(“MyCSV.csv”)  

CSV.to_html(“MyCSV.html”)  

HTML File Used: MyCSV

2. Converting HTML file to CSV using PDFKit Python API

There are many approaches for generating PDF in python. pdfkit is one of the better approaches as, it renders HTML into PDF with various image formats, HTML forms, and other complex printable documents.

We can create a PDF document with pdfkit in 3 ways. They are :

  • from URL
  • from a HTML file
  • from the string.

2.1. Generate PDF from URL: The following script gives us the pdf file from a website URL.

import pdfkit
pdfkit.from_url('https://www.geeksforgeeks.org', 'Output.pdf')

2.2. Generate PDF from file: The following script gives us the pdf file from an HTML file.

import pdfkit
pdfkit.from_file('LocalHTMLFile.html', 'Output.pdf')

2.3. Generate PDF from the string: The following script gives us the pdf file from a string.

import pdfkit
pdfkit.from_string('Geeks For Geeks', 'Output.pdf')

Since we have already converted our CSV file to HTML we will be using the first method i.e. Generating PDF from URL wherein either we can give any website’s address or any local HTML file.

If one already have wkhtmltopdf installed on machine we may use this syntax directly : 

Syntax for converting  HTML to PDF using PDFKit :

import pdfkit 

pdfkit.from_url(“MyCSV.html”, “FinalOutput.pdf”)

Else, we also need to install wkhtmltopdf for the script to run on our PC and set the installed file wkhtmltopdf.exe ‘s path to our PC’s Environment Variables and we can now skip the configuration section in the script.

or 

We can alternatively set Configuration as shown for the installed wkhtmltopdf.exe file and pass on the config variable to pdfkit.from_url function :

 Path Configuration

path_wkhtmltopdf = r’D:\Softwares\wkhtmltopdf\bin\wkhtmltopdf.exe’

config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)

Convert HTML file to PDF with pdfkit

pdfkit.from_url(“MyCSV.html”, “FinalOutput.pdf”, configuration=config)

Implementation:

Initial files in the folder

INITIAL FILES IN FOLDER

Python




import pandas as pd
import pdfkit
  
# SAVE CSV TO HTML USING PANDAS
csv = 'MyCSV.csv'
html_file = csv_file[:-3]+'html'
  
df = pd.read_csv(csv_file, sep=',')
df.to_html(html_file)
  
# INSTALL wkhtmltopdf AND SET PATH IN CONFIGURATION
# These two Steps could be eliminated By Installing wkhtmltopdf -
# - and setting it's path to Environment Variables
path_wkhtmltopdf = r'D:\Softwares\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
  
# CONVERT HTML FILE TO PDF WITH PDFKIT
pdfkit.from_url("MyCSV.html", "FinalOutput.pdf", configuration=config)


After Running Above Python Script :

FILES IN the SAME DIRECTORY AFTER RUNNING PYTHON SCRIPT

Final Output :



Previous Article
Next Article

Similar Reads

Convert PDF to CSV using Python
Python is a high-level, general-purpose, and very popular programming language. Python programming language (the latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. Python Programming Language is very well suited for Beginners, also for experienced programmer
2 min read
How to create multiple CSV files from existing CSV file using Pandas ?
In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting in
3 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI – tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma. Modules Needed Pandas: Python i
3 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
How to convert a PDF file to TIFF file using Python?
This article will discover how to transform a PDF (Portable Document Format) file on your local drive into a TIFF (Tag Image File Format) file at the specified location. We'll employ Python's Aspose-Words package for this task. The aspose-words library will be used to convert a PDF file to a TIFF file. Aspose-Words: Aspose-Words for Python is a pot
3 min read
Send PDF File through Email using pdf-mail module
pdf_mail module is that library of Python which helps you to send pdf documents through your Gmail account. Installing Library This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. pip install pdf-mail Function of pdf_mail This module only comes with a singl
2 min read
Python program to read CSV without CSV module
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
3 min read
How to Convert an image to NumPy array and saveit to CSV file using Python?
Let's see how to Convert an image to NumPy array and then save that array into CSV file in Python? First, we will learn about how to convert an image to a numpy ndarray. There are many methods to convert an image to ndarray, few of them are: Method 1: Using PIL and NumPy library. We will use PIL.Image.open() and numpy.asarray(). Example: C/C++ Code
4 min read
Convert Text File to CSV using Python Pandas
Let's see how to Convert Text File to CSV using Python Pandas. Python will read data from a text file and will create a dataframe with rows equal to number of lines present in the text file and columns equal to the number of fields present in a single line. See below example for better understanding. Dataframe created from upper text file will look
2 min read
Convert HTML table into CSV file in python
CSV file is a Comma Separated Value file that uses a comma to separate values. CSV file is a useful thing in today's world when we are talking about machine learning, data handling, and data visualization. In this article, we will discuss how to convert an HTML table into a CSV file. Converting HTML Table into CSV file in Python Example: Suppose HT
2 min read
Article Tags :
Practice Tags :