Python Project

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Creating payment receipts using Python

This Python project, is designed to generate a receipt in PDF


format. The receipt includes details such as the date, product
name, quantity, and price. The script prompts the user to input
these details, calculates the total price, applies a discount if
applicable, and then generates a well-formatted receipt.
The script uses the reportlab library to create the PDF
document. It sets up a table with a specific style, adds the data to
the table, and then builds the PDF document with a title and the
table. The title of the document is the name of the user, which is
input at the beginning of the script.
The script also includes a discount calculation feature. If the
subtotal of the prices is more than 999 and less than 2000, a
discount of 2.5% is applied. If the subtotal is 2000 or more, a
discount of 5% is applied. The final total is calculated by
subtracting the discount from the subtotal.
In summary, this script is a practical tool for generating detailed
receipts in PDF format, complete with automatic discount
calculations. It showcases the use of Python for tasks involving
user interaction, data manipulation, and document creation .
 Modules:

1. Importing Necessary Libraries: The script begins by importing necessary


libraries. reportlab is used for creating the PDF receipt, and datetime is
used for getting the current date and time.
2. Data Initialization: A list named DATA is initialized with column headers.
The name variable is set to the user’s input.
3. Product Details Input: The script prompts the user to enter the details of
various products (name, quantity, price). The user can enter multiple
products. The input process stops when the user enters ‘0’ as the product
name.
4. Subtotal Calculation: For each product, the script calculates the cost by
multiplying the quantity and price, and adds it to the sub_total.
5. Discount Calculation: The script calculates a discount based on the subtotal
of the purchase. If the subtotal is more than 999 and less than 2000, a
discount of 2.5% is applied. If the subtotal is 2000 or more, a discount of 5%
is applied.
6. Total Calculation: The total cost is calculated by subtracting the discount
from the subtotal.
7. Data Preparation: The subtotal, discount, and total cost are added to the
DATA list.
8. PDF Creation: The script creates a new PDF document using
SimpleDocTemplate from the reportlab.platypus library. The page size is
set to A4.
9. Styling: The script sets up styles for the PDF document using
getSampleStyleSheet and TableStyle from the reportlab library. The title
style is set to “Heading1” and aligned to the center.
10.Title and Table Creation: The script creates a Paragraph object for the title
and a Table object for the data. The title is set to the user’s name, and the
data is taken from the DATA list.
11.Building the PDF: Finally, the script builds the PDF document with the title
and table, and saves it as ‘receipt.pdf’ in the current directory.

This script is a practical demonstration of Python’s capabilities in document


generation and financial calculations. It can be used as a simple tool for generating
payment receipts for transactions. It also showcases the use of the reportlab
library for creating and styling PDF documents.
Project Lifecycle:

1. Initialization: The script begins by importing necessary libraries and


initializing the DATA list with column headers. It also captures the current
date and time, and initializes sub_total and discount variables to zero.
2. User Input: The script prompts the user to enter their name and the details
of various products (name, quantity, price). This process continues until the
user enters ‘0’ as the product name.
3. Data Collection and Calculation: For each product, the script appends the
product details to the DATA list and updates the sub_total by adding the cost
of the current product (quantity * price).
4. Discount Calculation: After all product details have been entered, the script
calculates a discount based on the sub_total. If the sub_total is more than
999 and less than 2000, a discount of 2.5% is applied. If the sub_total is
2000 or more, a discount of 5% is applied.
5. Total Calculation: The script calculates the total cost by subtracting the
discount from the sub_total.
6. Data Finalization: The sub_total, discount, and total are appended to
the DATA list.
7. PDF Document Creation: The script creates a new PDF document using
the SimpleDocTemplate class from the reportlab.platypus library. The
page size is set to A4.
8. Styling: The script sets up styles for the PDF document using the
getSampleStyleSheet and TableStyle classes from the reportlab library.
The title style is set to “Heading1” and aligned to the center.
9. Title and Table Creation: The script creates a Paragraph object for the title
and a Table object for the data. The title is set to the user’s name, and the
data is taken from the DATA list.
10.PDF Document Building: Finally, the script builds the PDF document with
the title and table, and saves it as ‘receipt.pdf’ in the current directory.

This lifecycle provides a step-by-step understanding of how the script works, from
initialization and user input, through calculations and data preparation, to the
creation and styling of the PDF document.
 Hardware Requirement:

 Processor –Core i5
 Hard Disk – 256 GB
 Memory – 4GB RAM
 Monitor

 Software Requirement:

 Windows 10 or higher
 Python
 Reortlab Module
 Datetime Module
Advantages

1. User Interaction: The script interacts with the user to


gather necessary data, making it adaptable to various
scenarios.
2. Dynamic Receipt Generation: It dynamically generates a
receipt based on user input, which means it can be used for
different products and quantities.
3. Discount Calculation: The script automatically calculates
discounts based on the total price, which simplifies the
billing process.
4. PDF Generation: The script uses the reportlab library to
generate a well-formatted PDF receipt. This makes the
receipt easy to print or email.
5. Data Validation: The script ensures that the quantity and
price inputs are numbers, preventing errors that could occur
from invalid data.
6. Flexibility: The script allows the user to enter multiple
products and calculates the total price accordingly. It also
allows the user to end the product entry process at any time.
7. Styling and Formatting: The script includes code for
styling and formatting the PDF receipt, resulting in a
professional-looking document.
8. Real-Time Date Capture: The script captures the current
date and time for each product entry, providing a real-time
record of when each product was added.
Limitation
1. Error Handling: The script lacks error handling. If a user
enters non-numeric values for quantity or price, the script
will crash due to a ValueError.
2. Input Validation: There’s no validation for negative or
zero values for quantity and price. This could lead to
incorrect calculations.
3. Discount Logic: The discount logic is hard-coded and only
applies to specific amounts. This limits flexibility if the
discount rules change.
4. Date Recording: The script records the time at the start of
the program for all products, instead of recording the time
each product is entered.
5. User Interface: The user interface is text-based and
requires manual input for each product. A graphical user
interface or a file-based input could be more user-friendly
and efficient.
6. Receipt Storage: The script overwrites the “receipt.pdf”
file each time it runs. This means you can’t keep a history
of all receipts unless you manually rename the file after
each run.
7. Code Comments: The script lacks comments, which can
make it difficult for others to understand the code’s purpose
and functionality.
8. Code Structure: The code could benefit from being broken
down into functions or classes. This would make it more
modular, easier to maintain, and potentially reusable in
other programs.
 Application
1. Retail Stores: This script can be used in small retail stores
to generate receipts for customers. It calculates the total
cost of items purchased, applies discounts based on
predefined rules, and generates a PDF receipt.
2. Restaurants: Restaurants can use this script to generate
bills for customers. The script can be modified to include
food items and their prices.
3. Wholesale Businesses: Wholesale businesses can use this
script to generate invoices for their clients. The script can
handle multiple items and calculate the total cost.
4. E-commerce Platforms: This script can be integrated into
e-commerce platforms to generate invoices for online
purchases.
5. Service Providers: Service providers such as freelancers,
consultants, or repair services can use this script to generate
invoices for their clients.

Please note that while this script can serve these purposes,
it would need to be modified and improved to handle real-
world scenarios effectively. For instance, it would need to
handle different types of inputs, errors, and exceptions. It
might also need to be integrated with a database to store
transaction data. Additionally, the user interface could be
improved for ease of use.
 Source Code
#MADE BY - JASHAN SINGH & AYUSH SINGH

from reportlab.platypus import SimpleDocTemplate, Table,


Paragraph, TableStyle

from reportlab.lib import colors

from reportlab.lib.pagesizes import A4

from reportlab.lib.styles import getSampleStyleSheet

from datetime import datetime

DATA = [

[ "Date" , "Name", "Quantity", "Price (Rs.)" ] ]

name=input("Enter Name : ")

print("Enter product name as 0 to end")

now=datetime.now()

date=now.strftime("%H:%M:%S")

sub_total , discount = 0 , 0

while(True):

l=[]

product=input("Enter product name : ")

if(product=="0") :

break
quantity=float(input("Enter quantity : "))

price=float(input("Enter the price of product : "))

l.append(str(date))

l.append(product.upper())

l.append(str(quantity))

l.append(str(price))

sub_total=sub_total+(price*quantity)

DATA.append(l)

if(sub_total>999 and sub_total<2000):

discount=round(sub_total*(0.025),2)

if(sub_total>=2000):

discount=round(sub_total*(0.050),2)

total=sub_total-discount

sblst=[]

sblst.append("Sub Total")

sblst.append(" ")

sblst.append(" ")

sblst.append(str(sub_total))
DATA.append(sblst)

dislst=[]

dislst.append("Discount")

dislst.append("2.5 % over 1000")

dislst.append("5% over 2000")

dislst.append(str(discount))

DATA.append(dislst)

totallst=[]

totallst.append("Total")

totallst.append(" ")

totallst.append(" ")

totallst.append(str(total))

DATA.append(totallst)

print(DATA)

pdf = SimpleDocTemplate( "receipt.pdf" , pagesize = A4 )

styles = getSampleStyleSheet()

title_style = styles[ "Heading1" ]


# 0: left, 1: center, 2: right

title_style.alignment = 1

title = Paragraph( name.upper() , title_style )

style = TableStyle(

( "BOX" , ( 0, 0 ), ( -1, -1 ), 3 , colors.black ),

( "GRID" , ( 0, 0 ), ( -1 , -1 ), 1 , colors.black ),

( "BACKGROUND" , ( 0, 0 ), ( 3, 0 ), colors.gray ),

( "TEXTCOLOR" , ( 0, 0 ), ( -1, 0 ), colors.whitesmoke ),

( "ALIGN" , ( 0, 0 ), ( -1, -1 ), "CENTER" ),

( "BACKGROUND" , ( 0 , 1 ) , ( -1, -1), colors.beige ),

table = Table( DATA , style = style )

pdf.build([ title , table ])


 Reference

 www.geeksforgeeks.org

 Python Programming : An Introduction to computer


science by JOHN ZELLE
 docs.reportlab.com
 datetime — Basic date and time types — Python 3.12.3
documentation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy