Python Script to Convert CSV File to HTML Table Using PrettyTable Library Full Tutorial For Beginners

Python Script to Convert CSV File to HTML Table Using PrettyTable Library Full Tutorial For Beginners

 

Welcome folks today in this blog post we will be converting csv file to html table using prettytable library full tutorial for beginners. All the source code of the tutorial will be given below.

 

 

Get Started

 

 

In order to get started we need to install the following library

 

pip install prettytable

 

 

Just install this prettytable library and now create a app.py file and copy paste the following code

 

 

from prettytable import PrettyTable 
  
  
# open csv file 
a = open("users.csv", 'r') 
  
# read the csv file 
a = a.readlines() 
  
# Seperating the Headers 
l1 = a[0] 
l1 = l1.split(',') 
  
# headers for table 
t = PrettyTable([l1[0], l1[1]]) 
  
# Adding the data 
for i in range(1, len(a)) : 
    t.add_row(a[i].split(',')) 
  
code = t.get_html_string() 
html_file = open('Table.html', 'w') 
html_file = html_file.write(code)

 

 

 

Now create a users.csv file inside your root directory of your project like this

See also  Python 3 replace() Script to Replace All Occurences in String Full Project For Beginners

 

 

name,age
gautam,23
kane,30

 

 

 

 

 

 

And if you run this script by executing the below command

 

 

python app.py

 

 

and now a Table.html file will be created in the root folder of your project like below.

 

 

 

Leave a Reply