Create a Student Report Card in Python - Prettytable Tutorial

a student report card in python using the prettytable library - PySeek

Introduction

In school life, I used to get the result of my studies throughout the year through a report card. A report card says how well we studied in a whole year.

Typically, we use Excel, Word or other such software to design a good report card. But did you know that there is a Python library(Prettytable) that allows us to create report card data in a table view format? Yes, you saw right.

In this python tutorial, I will show how you can create a student report card in python. It's an easy task, just keep reading this article until the end.

👉Visit Also: Communicate with Your Friends Secretly using Python

Requirements

Python offers a third-party library, named prettytable that allows us to display data in the form of a table format. We will use this library to generate students' report cards using the python program.

So before you start writing your code, make sure you have installed it.

Installation Process

Use pip3 instead of pip for Linux.

☛pip install prettytable

Create a Simple Report Card

First, we will create a simple report card by giving all the data manually in the code.

The Code


# A simple report card generation
from prettytable import PrettyTable
from prettytable import DOUBLE_BORDER

card = PrettyTable()
card.set_style(DOUBLE_BORDER)

name = "Alex Jones"
year = 2021
grade = "A"
teacher = "Mark Williamson"

print('\n')
print(' STUDENT REPORT CARD ')
print('NAME: ', name)
print('GRADE: ', grade)
print('TEACHER: ', teacher)
print('SCHOOL YEAR: ', year)

# Column Names of the table
card.field_names = ["SUBJECT", "Q1", "Q2", 'Q3', "GRADE SCALE"]

card.add_row(["Literature", "A", "B", "A+", "A+ 97-100"])
card.add_row(["Social Science", "B", "B", "A", "A 90-96"])
card.add_row(["Chemistry", "C", "C", "B+", "B+ 87-89"])
card.add_row(["Geography", "A", "C", "C", "B 80-86"])
card.add_row(["Physical Education", "B+", "A", "C", "C+ 77-79"])
card.add_row(["Entreprenurship", "A", "B", "A", "C 70-76"])
card.add_row(["Computer Application", "B+", "A", "A", "D+ 67-69"])
card.add_row(["History", "C", "B", "A", "D 60-66"])
card.add_row(["Algebra", "A", "A", "B", "F 1-59"])

print(card)

Output

showing report card data in tabular form using python prettytable library

Import the data from a CSV file

In this case, we will create the same report card by importing all the data from a CSV file instead of giving it manually.

The Code


# Import the data from a CSV file and print that data
from prettytable import from_csv
from prettytable import DOUBLE_BORDER

# Opening the CSV file using from_csv function
with open("report_card.csv") as fp:
report_card = from_csv(fp)

report_card.set_style(DOUBLE_BORDER)

print(report_card)

Output

showing report card data in the tabular form using python. In this case, the data has been fetched from a csv file.

Sorting the data

Now, we will sort the data of a report card based on a specific attribute. In this case, the attribute will be the "PERCENTAGE". You can use other also, just need to mention the attribute name as it is, present in the file.

The Code


# Import the data from a CSV file and print that data
from prettytable import from_csv
from prettytable import DOUBLE_BORDER

# Opening the CSV file using from_csv function
with open("report_card.csv") as fp:
report_card = from_csv(fp)

report_card.set_style(DOUBLE_BORDER)


# Printing the report card table sort by student's percentage
print(report_card.get_string(sortby="PERCENTAGE"))

Output

sorted data of a report card and displaying this on the terminal in the tabular form using python.

👉Visit Also: Convert Text File to QR Code using Python

Summary

In this tutorial, we learned how to generate a report card of a student in the tabular form using the python prettytable library.

First, we put all the data manually in the program, later, we did the same thing by fetching all the data from a CSV file instead of doing manually. At the end, we learned to sort those data based on a specific attribute.

I hope You've enjoyed this tutorial. Please share Your love❤️ and drop Your comment below. You'll get a reply soon.

Get more unique programming examples from here.

👉Censor Bad Words from a text - Profanity filter in Python

👉Hack with Image: Extract Metadata from an Image using Python

👉Python Keylogger - Build a Simple Keylogger in Python

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

1 Comments

  1. i am a computer science student of cbse board class 12 , i cant do programming because i dont know basic can you teach or an you give some imformation which is import topic for learning as my first programming language is python . before that i havent learned C++ and JAVA



    I HOPE FOR A POSITIVE REPLY

    ReplyDelete
Post a Comment
Previous Post Next Post