Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert CSV File to PDF File using Python?
In today's data-driven world, being able to convert CSV files to more presentable PDF format is a common requirement. Python provides powerful libraries that make this conversion process straightforward and efficient.
This tutorial demonstrates how to convert CSV files to PDF using Python by first converting the CSV to HTML format using pandas, then converting the HTML to PDF using pdfkit.
Required Libraries and Setup
Before starting, you'll need to install the required libraries and the wkhtmltopdf utility ?
pip install pandas pdfkit
You also need to install wkhtmltopdf from: https://wkhtmltopdf.org/downloads.html
Sample CSV Data
Let's work with a sample CSV file named inputs.csv containing employee data ?
Name,Age,Occupation John,32,Engineer Jane,28,Teacher Bob,45,Salesperson
Method 1: Converting CSV to HTML
First, we'll convert the CSV file to HTML format using pandas ?
import pandas as pd
# Read the CSV file into a pandas dataframe
df = pd.read_csv('inputs.csv')
# Convert the dataframe to an HTML table
html_table = df.to_html(index=False)
# Print the HTML table
print(html_table)
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th>Name</th>
<th>Age</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>32</td>
<td>Engineer</td>
</tr>
<tr>
<td>Jane</td>
<td>28</td>
<td>Teacher</td>
<tr>
<td>Bob</td>
<td>45</td>
<td>Salesperson</td>
</tr>
</tbody>
</table>
Method 2: Converting HTML to PDF
Next, we'll use pdfkit to convert the HTML table to a PDF file ?
import pdfkit
import pandas as pd
# Read CSV and convert to HTML
df = pd.read_csv('inputs.csv')
html_table = df.to_html(index=False)
# Configure PDF options
options = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in'
}
# Convert HTML to PDF
pdfkit.from_string(html_table, 'output.pdf', options=options)
print("PDF created successfully!")
Complete Solution with Styling
Here's a complete solution that creates a well-formatted PDF with custom styling ?
import pandas as pd
import pdfkit
# Read CSV file
df = pd.read_csv('inputs.csv')
# Create styled HTML
html_string = f'''
<html>
<head>
<style>
table {{
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}}
th, td {{
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}}
th {{
background-color: #f2f2f2;
font-weight: bold;
}}
h1 {{
color: #333;
text-align: center;
}}
</style>
</head>
<body>
<h1>Employee Data Report</h1>
{df.to_html(index=False, table_id="employee-table")}
</body>
</html>
'''
# PDF options
options = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
# Generate PDF
pdfkit.from_string(html_string, 'styled_output.pdf', options=options)
print("Styled PDF created successfully!")
Key Parameters
| Parameter | Function | Example Values |
|---|---|---|
index=False |
Removes row index from HTML table | True/False |
page-size |
Sets PDF page size | A4, Letter, Legal |
margin-* |
Sets page margins | 0.75in, 10mm, 20px |
Common Issues and Solutions
wkhtmltopdf not found: Ensure wkhtmltopdf is installed and added to your system PATH, or specify the path explicitly ?
config = pdfkit.configuration(wkhtmltopdf='/path/to/wkhtmltopdf') pdfkit.from_string(html_string, 'output.pdf', configuration=config)
Conclusion
Converting CSV to PDF in Python involves two main steps: converting CSV to HTML using pandas, then converting HTML to PDF using pdfkit. This approach provides flexibility to customize the PDF appearance with CSS styling and various formatting options.
