Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Generate Certificate Using Python
Rahul Sinha
Rahul Sinha

Posted on

     

Generate Certificate Using Python

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 toimport 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 importpandas

import pandas as pd

After importing pandas, we have to importpillow package for usingImage andFont

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 aname_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 usingImage.open. Here is the screenshot of the demo certificate.
Alt Text

One thing I want to clear thatfor i in name_listi 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 toview section and tick thestatus bar. It shows the position of the mouse on the image on the down side.

Alt Text

Then give the text color inRGB. Then infont variable type the font name you want to use and using comma type thefont size.

In the last line, you see the code

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

It will save all your certificates in PDF manner. Herei is the name to the attendee. It will save likecertificate_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

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
srosuna1 profile image
Samuel R Osuna
un aprendiz
  • Education
    media
  • Work
    freelance
  • Joined

Hi, what if I need a CSV file separated by commas instead of an XLSX file? what would the code look like?
thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Technology enthusiast. Keen interest is in Python, Voice technology, Azure, and IoT. Keen to learn new things.
  • Location
    India
  • Work
    Programmer at Student
  • Joined

More fromRahul Sinha

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp