loading...
Cover image for Generate Certificate Using Python

Generate Certificate Using Python

rahulsinha036 profile image Rahul Sinha ・3 min read

Most have you thought that I could generate a certificate automatically for the attendees. This was my problem also because being a Microsoft Student Partner I tool many virtual events. So creating certificates are the most headache things I ever saw.
Then I got the idea to develop a program that generates certificates automatically. I go with python because it is very dynamic in use and when we create very long codes in a short format in python.

Let's Start writing our Code!

The first things are we have to import pandas. Because we will be dealing with the excel sheets where are the names of the attendees are there. Like I have a created a demo excel sheet.
Alt Text

Let's import pandas

import pandas as pd

After importing pandas, we have to import pillow package for using Image and Font

from PIL import Image, ImageDraw, ImageFont

Now, we have read our excel file. We will use the panda read_excel function here to read the data.

data = pd.read_excel(r,'C:\Users\Rahul sinha\names.xlsx')
  • Note - place "r" before the path string to address special character, such as '\'. Place your whole where your file is saved.

Now create a name_list variable where all the names of the attendees will be saved.

name_list = data["Name"].tolist() 

Here, you see "Name", it is the name of the column under which all the attendee names are there. Basically we are indexing all the names. Then converting all the names to the list.

Now we have to use a loop to print all the certificates of the attendees present in the excel sheet.
I am sharing the whole code.

for i in name_list:
    im = Image.open(r'C:\Users\Rahul sinha\certificate_img.jpg')
    d = ImageDraw.Draw(im)
    location = (100, 398)
    text_color = (0, 137, 209)
    font = ImageFont.truetype("arial.ttf", 120)
    d.text(location, i, fill = text_color, font = font)
    im.save("certificate_" + i + ".pdf")

I am explaning the whole code one by one. First I open the certificate image using Image.open. Here is the screenshot of the demo certificate.
Alt Text

One thing I want to clear that for i in name_list i is all the names of the attendees.
For the location, we can use paint to point where our names should be printed. Open Paint. Go to view section and tick the status bar. It shows the position of the mouse on the image on the down side.

Alt Text

Then give the text color in RGB. Then in font variable type the font name you want to use and using comma type the font size.

In the last line, you see the code

im.save("certificate_" + i + ".pdf")

It will save all your certificates in PDF manner. Here i is the name to the attendee. It will save like certificate_Rahul Sinha.pdf

  • Note - All the PDF file save where your python code will be.

Let's run our code and see the output.
Alt Text

You can see in the image all the names in the excel sheet have been printed. Let's see the final output by opening the PDF.

Alt Text

Hurray!! You successfully generated your certificate using Python. I am a working GUI Certificate Generator Using Python. It will be an open-source project. If you love this blog do share with your friends and team-mates

Posted on May 21 by:

rahulsinha036 profile

Rahul Sinha

@rahulsinha036

Technology enthusiast. Keen interest is in Python, Voice technology, Azure, and IoT. Keen to learn new things.

Discussion

markdown guide