How I Use Python to Automate My Cover Letters

Project Description

I was recently thinking of applying for a job change. Many of my friends have already acquired numerous offer letters of late with lucrative salary packages. I was itching to try my luck out in the industry as well. But time and tide wait for none, and I didn’t have much time left to apply for the vacancies that had come up. I was panicking and wanted to finish 100 cover letters in a flash.

However, writing 100 cover letters would atleast need 4 hours and my daily schedule was already jam-packed. That is when I came up with the idea of automating the creation of my cover letters which would save me hours of manual labor.

Thus, in this mini project, I will be demonstrating how I automated the task of creating my cover letters by merely feeding in the name of the organization I was applying to and the position was applying for.

Prerequisites: So, here are the three things that you need before starting to automate your task –

  • Microsoft Word
  • A Master cover letter (template)
  • Python with a package manager

So, without further delay, let us dive into the different steps of automating our cover letter creation.

Video Walkthrough

Step 1: Fetching The Master Cover Letter

You will first need a master cover letter template so that you can modify it accordingly. I am going to use a cover letter that I fetched from Indeed and the position being applied for is IT director. Here’s the link to the cover letter that has been used in this demonstration: https://www.indeed.com/career-advice/cover-letter-samples/it-director-cover-letter

Copy and paste the content into Microsoft Word and then modify it to replace all occurrences of date, organization name and position with their respective variables within double curly braces.

  • Replace the date with {{ today_date }}.
  • Replace the organization name with {{ company_name }}.
  • Replace the position you are applying for with {{ position_name}}.

Now every time you run your script, all you need to do is simply feed in the company name and the position and that’s it. So let’s move on to the next step.

Step 2: Import and Install the Necessary Libraries

Our code uses the “docxtpl” library to generate a personalized cover letter. The “docxtpl” library allows you to create a Microsoft Word document from a template, where placeholders in the template can be replaced with dynamic data.

Overall, the “docxtpl” library is a powerful tool that allows you to easily generate personalized documents in Microsoft Word format. With a bit of knowledge and practice, you can use it to automate many tasks related to document generation.

To install the docxtpl open your terminal and type the following command – pip install docxtpl

That’s it! We also need the datetime module which is also a built-in module in Python. So, once you have already installed the required library, go ahead and import them within your script.

from docxtpl import DocxTemplate
import datetime

Step 3: The Code to Populate the Placeholders

company_name = input("Enter name of the Company : ")
position_name = input("Enter name of the Position: ")
today_date = datetime.datetime.today().strftime('%m/%d/%Y')
context = { 'today_date': today_date,
           'company_name' : company_name,
           'position_name' : position_name}
# Open our master template
doc = DocxTemplate("master_cover_letter.docx")
# Load them up
doc.render(context)
# Save the file with personalized filename
doc.save('Cover_letter_'+company_name+'_'+position_name+'.docx')

Explanation:

Our code prompts the user to enter the name of the company and the position they are applying for using the “input” function. The current date is obtained using the “datetime” module and formatted as a string in the format of ‘%m/%d/%Y‘. The user inputs and the today’s date are then stored in a dictionary called “context” with keys ‘today_date‘, ‘company_name‘ and ‘position_name‘.

We then need to use the “DocxTemplate” class to open a master template document called “master_cover_letter.docx“. This document should be located in the same directory as your Python script and should contain placeholders that will be replaced with the dynamic data. The “render” method is used to fill in the placeholders in the template with the values from the “context” dictionary.

Finally, we have to use the “save” method to save the filled-in template as a new document, with a personalized filename that includes the company and position names. The filename is constructed by concatenating the string ‘Cover_letter_‘, the value of the ‘company_name‘ key, ‘_‘ and the value of the ‘position_name‘ key, with a ‘.docx‘ extension.

Putting it All Together

Let’s put it all together to visualize how the entire script works –

from docxtpl import DocxTemplate
import datetime

company_name = input("Enter name of the Company : ")
position_name = input("Enter name of the Position: ")
today_date = datetime.datetime.today().strftime('%m/%d/%Y')
context = { 'today_date': today_date,
           'company_name' : company_name,
           'position_name' : position_name}
# Open our master template
doc = DocxTemplate("master_cover_letter.docx")
# Load them up
doc.render(context)
# Save the file with personalized filename
doc.save('Cover_letter_'+company_name+'_'+position_name+'.docx')

Note that the two important criteria to be kept in mind is that to use this code, you will need to have a “master_cover_letter.docx” template file in the same directory as your Python script. Additionally, you will need to have the “docxtpl” library installed in your environment. Once you execute the code the newly created cover letter will appear in the same folder as the project folder.

It’s also important to note that this script is written to be executed in the command line. If you want to use it in a web application, you will have to make changes accordingly.

Conclusion

My code provides a simple example of how to use the “docxtpl” library to generate a personalized document from a template. You can modify this code to include more placeholders and keys in the context dictionary to include more personalized information in the document. Also, you can use different input methods like reading from a file, databases or web scraping, etc.

With that, we come to the end of this project.  I hope this project added some value and helped you in your coding quest. Stay tuned and subscribe for more interesting projects and tutorials.

Do you love automating tasks with Python? Well! I do. And if you are someone who likes automation, then here’s a list of a few mini projects that will get you going –