How To Generate ID Card with python code

How To Generate ID Card with python code


simple clone it
 git clone  https://github.com/RedaElmar/ID-Cards-Generator


import pandas as pd 
import pdfkit
from datetime import datetime, timedelta
import code128
import random

 

Functions

#generate costum html file using the arguments
def HTMLgen(code,first_name,last_name,function,date_of_birth,picture,expiration_date):
    html= r"""<!doctype html><meta charset="utf-8"><link rel="stylesheet" href="../RES/card.css"><body><div class="face face-front" ><img src="../RES/front.png"></div><div id="infoi"><img src="@picture" height="89.5" width="83" />
        <div style="margin-left: 1.3cm;margin-top: -0.6cm;">
            <br>
            <div style="font-size: 0.7em;margin-top: 5%;font-family: sans-serif;color: aliceblue;text-transform: uppercase;"><b>@fname</b> @lname</div><br>
        <div style="font-size: 0.7em;margin-top: -0.4cm;font-family: sans-serif;color: aliceblue;text-t ransform: capitalize;">@function</div>
        </div>
    </div>
    <div id="info">
        <br><div style="font-size: 0.7em;margin-top: 0.6%;font-family: sans-serif;text-transform: uppercase;">@code</div>
        <br><div style="font-size: 0.7em;margin-top: -0.6%;font-family: sans-serif;text-transform: capitalize;">@date_of_birth</div>
        <br><div style="font-size: 0.7em;margin-top: -0.6%;font-family: sans-serif;text-transform: capitalize;">@expiration_date</div>
    </div>
    <div id="BARCODE"><img src="../RES/bar.PNG"  height="20" width="120"/></div>

</body>"""
    html = html.replace("@picture",picture)
    html = html.replace("@code",str(code))
    html = html.replace("@fname",first_name)
    html = html.replace("@lname",last_name)
    html = html.replace("@function",function)
    html = html.replace("@date_of_birth",date_of_birth)
    html = html.replace("@expiration_date",expiration_date)
    f= open("index.html","w")
    f.write(html)
    f.close()
    return 
#generate a file bar.png contains barcode of the 10 digits code  
def BARgen(code):
    code128.image(code).save("../RES/bar.png")
    return
#generate costum pdf file using the existing html file // code argument is only to name the file generated
def PDFgen(code):
    config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
    options = {'dpi': 365,'margin-top': '0in','margin-bottom': '0in','margin-right': '0in','margin-left': '0in','page-size': 'A8',"orientation": "Landscape",'disable-smart-shrinking': ''}
    pdfkit.from_file('index.html', str(code)+".pdf",configuration=config , options = options)
    return       
#complete with random digits an integer x until it contains 10 digits
def complete(x):
    x=str(x)
    while len(x)<10:
            x+=str(random.randint(0,9))
    return int(x)

 

Import Dataset

data=pd.read_excel("../RES/exo_dataset_P001.xlsx")

 

Show Sample

data.head(5)
  code first_name last_name function date_of_birth submission_date picture
0 5591927432 Prent Hickenbottom Senior Editor 4/3/1993 22/1/2019 https://robohash.org/quidemsaepequi.png?size=3...
1 5071682362 Jard Stein Geological Engineer 15/7/1992 10/4/2019 https://robohash.org/teneturautsunt.png?size=3...
2 6295398980 Levon Noen Research Assistant IV 12/10/1999 27/3/2019 https://robohash.org/quaeetsed.png?size=300x30...
3 3718164078 Nonah Scoggin Junior Executive 9/7/2000 21/1/2019 https://robohash.org/debitisinciduntdolore.png...
4 2261412840 Jameson Hugueville Teacher 5/12/1991 1/2/2019 https://robohash.org/facilisrerumad.png?size=3...

some of the code column entries are only 9 digits or less, so we will add some random degits to them.

data['code'] = data['code'].apply(lambda x: complete(x))
data.dtypes

code int64
first_name object
last_name object
function object
date_of_birth object
submission_date object
picture object
dtype: object

 

convert to datetime elements

data['submission_date']=pd.to_datetime(data['submission_date'])
data['date_of_birth'] = pd.to_datetime(data['date_of_birth'])
data.dtypes

code int64
first_name object
last_name object
function object
date_of_birth datetime64[ns]
submission_date datetime64[ns]
picture object
dtype: object

data.head(5)
  code first_name last_name function date_of_birth submission_date picture
0 5591927432 Prent Hickenbottom Senior Editor 1993-04-03 2019-01-22 https://robohash.org/quidemsaepequi.png?size=3...
1 5071682362 Jard Stein Geological Engineer 1992-07-15 2019-10-04 https://robohash.org/teneturautsunt.png?size=3...
2 6295398980 Levon Noen Research Assistant IV 1999-12-10 2019-03-27 https://robohash.org/quaeetsed.png?size=300x30...
3 3718164078 Nonah Scoggin Junior Executive 2000-09-07 2019-01-21 https://robohash.org/debitisinciduntdolore.png...
4 2261412840 Jameson Hugueville Teacher 1991-05-12 2019-01-02 https://robohash.org/facilisrerumad.png?size=3...

 

add expiration date column (submission date + 15 days)

data["expiration_date"] = data["submission_date"] + timedelta(days=15)

 

Controlling date form DD-MM-YYYY

data['date_of_birth']=data['date_of_birth'].dt.strftime('%d-%m-%Y')
data['submission_date']=data['submission_date'].dt.strftime('%d-%m-%Y')
data['expiration_date']=data['expiration_date'].dt.strftime('%d-%m-%Y')
data.head(5)
  code first_name last_name function date_of_birth submission_date picture expiration_date
0 5591927432 Prent Hickenbottom Senior Editor 03-04-1993 22-01-2019 https://robohash.org/quidemsaepequi.png?size=3... 06-02-2019
1 5071682362 Jard Stein Geological Engineer 15-07-1992 04-10-2019 https://robohash.org/teneturautsunt.png?size=3... 19-10-2019
2 6295398980 Levon Noen Research Assistant IV 10-12-1999 27-03-2019 https://robohash.org/quaeetsed.png?size=300x30... 11-04-2019
3 3718164078 Nonah Scoggin Junior Executive 07-09-2000 21-01-2019 https://robohash.org/debitisinciduntdolore.png... 05-02-2019
4 2261412840 Jameson Hugueville Teacher 12-05-1991 02-01-2019 https://robohash.org/facilisrerumad.png?size=3... 17-01-2019

 

Generating Costumized Pdf files from the dataset

for index, row in data.head(n=5).iterrows()    :
    code=row[0]
    fname = row[1]
    lname = row[2]
    func  = row[3]
    dob   = row[4]
    ed    = row[5]
    pic   = row[6]
    BARgen(code)
    HTMLgen(code,fname,lname,func,dob,pic,ed)
    PDFgen(code)
    

Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

 

generated 5 pdf files with thier corresponding enteries

 

Thank you

About author
teachbang
teachbang
Admin @ TeachBang I’m passionate about building great products that make people’s lives easier. Experienced cyber security,ethical hacking,Machine Learning and Web development professional with over 3 years strategizing innovative digital industry experiences.
Comments
Do you want to get informed about new articles?

Related articles