CSV files contain comma-separated values that usually contain tabular. Sometimes, we might need to covert a csv file into a PDF file. In this article, we will discuss how we can convert a csv file to PDF in python.
How to Convert CSV to PDF File in Python?
To convert a csv file to PDF, we will first create an HTML string of the contents of the csv file using the pandas module. The pandas module provides us with different tools to handle csv files.
To convert a csv file to an HTML string, we will first open the file using the read_csv()
method. The read_csv()
method takes the file name of the csv file as an input argument and returns a dataframe containing the data from the csv file.
After obtaining the data from the csv file into the dataframe, we can convert the dataframe into an HTML string using the to_html()
method. The to_html()
method, when invoked on a dataframe, converts the dataframe into an HTML table and returns the HTML text in the form of a string. You can observe this in the following example.
import pandas as pd
df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
html_string = df1.to_html()
print("The html string is:")
print(html_string)
Output:
The dataframe is:
Name Roll Number Subject
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
The html string is:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Name</th>
<th>Roll Number</th>
<th>Subject</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Aditya</td>
<td>12</td>
<td>Python</td>
</tr>
<tr>
<th>1</th>
<td>Sam</td>
<td>23</td>
<td>Java</td>
</tr>
<tr>
<th>2</th>
<td>Chris</td>
<td>11</td>
<td>C++</td>
</tr>
<tr>
<th>3</th>
<td>Joel</td>
<td>10</td>
<td>JavaScript</td>
</tr>
<tr>
<th>4</th>
<td>Mayank</td>
<td>5</td>
<td>Typescript</td>
</tr>
</tbody>
</table>
After obtaining the csv file in the form of an HTML string, we will convert the HTML string to a pdf file. For this, we will use the pdfkit module, which is built upon the wkhtmltopdf library. The pdfkit module provides us with the from_string()
method that we can use to convert the HTML string to a pdf file. For this, we will use the from_string()
method. The from_string()
method takes the HTML string as its first input argument and the file name of the pdf file as its second input argument. After execution, the HMTL string is saved in the pdf file. You can observe this in the following example.
import pandas as pd
import pdfkit
df1 = pd.read_csv('student_details.csv')
print("The dataframe is:")
print(df1)
html_string = df1.to_html()
pdfkit.from_string(html_string, "output_file.pdf")
print("PDF file saved.")
Output:
The dataframe is:
Name Roll Number Subject
0 Aditya 12 Python
1 Sam 23 Java
2 Chris 11 C++
3 Joel 10 JavaScript
4 Mayank 5 Typescript
PDF file saved.
Attached is the PDF file created from the csv file.
Conclusion
In this article, we have discussed how to convert a csv file to a pdf file in python. To know more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.