Generate Certificate Using Python
Tags: python, computerscience, githubsdp, github
Published: 2020-05-21 07:18:59

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](https://dev-to-uploads.s3.amazonaws.com/i/72myrjmnb77t4ebllvco.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/o6pwe44qigddsgzzxd4g.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/homjkmw6llp7gwbotluy.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/93rc03zus2ejk95gt4z9.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/lwn5k2ityvlx3ctbyzl1.png)

* Here is repo on GitHub of this code
{% github rahulsinha036/Generate-Certificate-using-Python3 %}

###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