Make a certificate creator using Python

Doesn’t the title sound so cool?
But don’t be scared, it is not at all difficult, a very simple script can perform the functionality.

Here is a link to my Certificate sender project. It sends out certificates using Email to all the participants registered in a spreadsheet. https://github.com/kunalgrover05/Certificate-sender

Let’s get started and understand how to make the certificate creator. The first thing you need is a template. You can create one very easily using Microsoft Word or OpenOffice or on a lot of websites.

Once you have it, we can start with the python part. We will be using the Python Imaging Library(PIL) here. You need to first get the data for the certificate, ie the Name and the other fields that need to be entered.

Once you have that, let’s hack the template!!

First import the library modules using

  from PIL import Image
  from PIL import ImageFont
  from PIL import ImageDraw

Now we will open the template and initiate a draw object

  img = Image.open("Template.jpg")
  draw = ImageDraw.Draw(img)

Now let’s load a font file. It searches the system paths for the fonts and fails if not available. It is always best to copy the font file to the script directory

  selectFont = ImageFont.truetype("FontName.ttf", size = 60)

Now insert text into image template

  draw.text( (x,y), text, (r,g,b), font=selectFont
# (x,y) is the starting position for the draw object
# text is the text to be entered
# (r,g,b) represents the color eg (255,0,0) is Red
# font is used to specify the Font object

Now save the certificate.
Save as a PDF

  img.save( 'certi.pdf', "PDF", resolution=100.0)

Note that PIL can be used to save a lot of other formats as well, refer to http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html for more details.

Now you have a generated certificate, you can easily send it by mail and guess what you can even automate the sending process in python! Check out my project for more details.
Another important thing I considered in my project was to shorten names. 😛
It becomes important otherwise the names will overflow the template size. So it is a good idea to do something of this sort or maybe select the font size depending on the text’s length.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s