• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Convert CSV to PDF file in python

Convert CSV to PDF file in python

Author: Aditya Raj
Last Updated: July 10, 2022

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.

output_fileDownload

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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us