Automatic Certificate Generator in Python

Automatic Certificate Generator in Python

Β·

3 min read

Hello guys in this tutorial, I am going to tell you how you can create an automatic certificate generator in python. So for that first, you need to create a text file, having some names as we as email addresses. image.png Now, find a sample certificate as shown below (you can easily find free sample certificates on canva.com ) and add the sample certificate to the same folder.

Yellow and Green Chalkboard Kindergarten Diploma Certificate.png Then, also create a subfolder and rename it as 'certificates' (you can see your all generated certificates inside this folder) and finally create a python file and save it to the same folder. image.png Now let's deep dive into the coding part, first of all you need to import some of the module modules:-

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from PIL import Image, ImageDraw, ImageFont

Now, create a function gen_cert() and read a text file that contains names and emails.

def gen_cert():
    with open('sample_name.txt') as f:
        content = f.readlines()
        content = [x.strip() for x in content] #to remove whitespace

Now create a for loop and create two lists for names and emails for the email function. And, open a sample certificate from the folder

sample_name = []
sample_email = []
for i in content:
    s = i.split(',')
    sample_name.append(s[0])
    sample_email.append(s[1])
    font = ImageFont.truetype('arial.ttf',60) 
    sample_image = Image.open('sample_certi.png')

Now in the same loop,

draw = ImageDraw.Draw(sample_image)
draw.text(xy=(450,350),text='{}'.format(s[0]),fill=(0,0,0),font=font) 
sample_image.save('certificates/{}.png'.format(s[0]))

Now in the same loop, we used ImageDraw to draw text in the sample certificate image.

draw = ImageDraw.Draw(sample_image)
draw.text(xy=(450,350),text='{}'.format(s[0]),fill=(0,0,0),font=font)
#xy= Top left corner of the text,text= Text to be drawn,fill= color to use for the text

Now to save the image first set the path, In my case, it's 'certificates', and then it saves the certificate according to the name of the person.

sample_image.save('certificates/{}.png'.format(s[0]))

And, also add sample_email and sample_name to a global variable

global sample_email,sample_name

Now for sending mail, we need to create one more function send_mail() here we use our global list (sample_name (for attachments) and sample_emails (for people to send mail)). And then create an email sender with the help of smtplib and mime. In the next article, I am gonna tell you in detail about how you can send emails with python.

def send_mail():
        for i,j in zip(sample_name,sample_email):
            email_user = 'Enter your email address'
            email_password = 'Enter your password'
            subject = 'Certificate Of Participation'
            msg = MIMEMultipart()
            msg['From'] = email_user 
            msg['To'] = j 
            msg['Subject'] = subject #for subject
            body = 'Hello world' 
            msg.attach(MIMEText(body,'plain'))
            filename = 'certificates/{}.png'.format(i) #for attachments
            attachment  =open(filename,'rb')
            part = MIMEBase('application','octet-stream')
            part.set_payload((attachment).read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',"attachment; filename= "+filename)
            msg.attach(part)
            text = msg.as_string()
            server = smtplib.SMTP('smtp.gmail.com',587) 
            server.starttls()
            server.login(email_user,email_password) #login
            server.sendmail(email_user,j,text) 
            server.quit()

And at last call both functions gen_cert() and send_mail()

gen_cert()
send_mail()

FULL CODE: github.com/dx4iot/certificate-generator/blo..

Output: image.png

Nishant.png

Hope you liked this article πŸ™‚

Did you find this article valuable?

Support Nishant Tiwari by becoming a sponsor. Any amount is appreciated!