my_prod={1:['Hard Disk',80],2:['RAM',90],3:['Monitor',75],
4:['CPU',55],5:['Keyboard',20],6:['Mouse',10],
7:['Mother board',50],8:['Power Sypply',20],
9:['Speaker',50],10:['Microphone',45]}
my_sale is a dictionary having product_id as key and quantity sold as value. my_sale={1:2,3:2,7:1,4:3,6:5,5:3,2:1,9:1,10:3}
We have discount and tax rate as variables inside this file.
discount_rate=10 # 10% discount
tax_rate=12 # tax rate in percentage
my_prod
The product list and billing details used in this example (my_prod
) can be
dynamically loaded from a database, Excel sheet, or
CSV file, then passed directly into the invoice script. Use libraries such as
sqlite3
/SQLAlchemy
for databases, pandas
for Excel/CSV, or
Python’s built-in csv
module to read records and assemble the same structure expected by
my_prod
. This lets you automate PDF invoice creation from your live order data.
from temp_invoice import my_temp # import the template
from invoice_data import * # get all data required for invoice
By using for loop all sales data are displayed with quanity and price. Here for price and total drawRightString() is used.
for items in my_sale:
c.drawString(0.1*inch,line_y*inch,str(my_prod[items][0])) # p Name
c.drawRightString(4.5*inch,line_y*inch,str(my_prod[items][1])) # p Price
c.drawRightString(5.5*inch,line_y*inch,str(my_sale[items])) # p Qunt
sub_total=my_prod[items][1]*my_sale[items]
c.drawRightString(7*inch,line_y*inch,str(sub_total)) # Sub Total
total=round(total+sub_total,1)
line_y=line_y-row_gap
invoice_data
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.