Creating payment receipts using Python - GeeksforGeeks

24 Jun, 2021
Creating payment receipts is a pretty common task, be it an e-commerce website or any local store for that matter.
Here, we will see how to create our own transaction receipts just by using python. We would be using reportlab to generate the PDFs. Generally, it comes as a built-in package but sometimes it might not be present too. If it’s not present, then simply type the following in your terminal
pip install reportlab
Import module.
We need the data in the form of a list of lists. The 0th index of the outer index is the headers.
We create a simple doc template with the specified paper size (here A4)
Then get a sample style sheet from the built-in style sheets and add the styling accordingly.
After you have created a style object, feed in the data and the style sheet to the pdf object and build it.
Below is the implementation:
Python3
# imports modulefrom reportlab.platypus import SimpleDocTemplate, Table, Paragraph, TableStylefrom reportlab.lib import colorsfrom reportlab.lib.pagesizes import A4from reportlab.lib.styles import getSampleStyleSheet # data which we are going to display as tablesDATA = [    [ "Date" , "Name", "Subscription", "Price (Rs.)" ],    [        "16/11/2020",        "Full Stack Development with React & Node JS - Live",        "Lifetime",        "10,999.00/-",    ],    [ "16/11/2020", "Geeks Classes: Live Session", "6 months", "9,999.00/-"],    [ "Sub Total", "", "", "20,9998.00/-"],    [ "Discount", "", "", "-3,000.00/-"],    [ "Total", "", "", "17,998.00/-"],] # creating a Base Document Template of page size A4pdf = SimpleDocTemplate( "receipt.pdf" , pagesize = A4 ) # standard stylesheet defined within reportlab itselfstyles = getSampleStyleSheet() # fetching the style of Top level heading (Heading1)title_style = styles[ "Heading1" ] # 0: left, 1: center, 2: righttitle_style.alignment = 1 # creating the paragraph with# the heading text and passing the styles of ittitle = Paragraph( "GeeksforGeeks" , title_style ) # creates a Table Style object and in it,# defines the styles row wise# the tuples which look like coordinates# are nothing but rows and columnsstyle = TableStyle(    [        ( "BOX" , ( 0, 0 ), ( -1, -1 ), 1 , colors.black ),        ( "GRID" , ( 0, 0 ), ( 4 , 4 ), 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 ),    ]) # creates a table object and passes the style to ittable = Table( DATA , style = style ) # final step which builds the# actual pdf putting together all the elementspdf.build([ title , table ])
Output:
receipt.pdf
adnanirshad158
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()