How to make a holiday calendar step by step

4 min read

Have you thought about how many company holidays are available? Do you often check when the next holiday is? Would it be nice to have a calendar full of the holidays for the year? Today we will create a holiday calendar using Python using the Nylas APIs and visualize with Obsidian (A powerful knowledge base). 

What are we going to talk about?

Step 1 – Getting started

To install Python (if you don’t have it installed already), install the Nylas Python SDK, set up the .env file and get your tokens for the dashboard. Also, read the blog post How to Send Emails with the Nylas Python SDK for further details on setup.

To install Obsidian (as we will generate a Markdown file), follow their instructions.

Our .env file will contain the following information:

CLIENT_ID = "<CLIENT_ID>"
CLIENT_SECRET = "<CLIENT_SECRET>"
ACCESS_TOKEN = "<ACCESS_TOKEN>"
CALENDAR_ID = “<COMPANY_HOLIDAYS_CALENDAR>”

Step 2 – Creating the Python script

Create a file and call it Holidays_Generator.py with the following code:

# Import your dependencies
import os
import sys
import re
import calendar
from datetime import date
import datetime
from nylas import APIClient
from dotenv import load_dotenv

# Load your env variables
load_dotenv()

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Array to hold all events
yearly_events = []
# Months of the year
months = range(1, 13)
# Year that we want to print out
year = str(sys.argv[1])

# Path of file
file_name = (
    "<YOUR_OBSIDIAN_PATH>/Documents/Blag/Holidays Calendar "
    + year
    + ".md"
)

START_TIME = int(datetime.datetime(int(year), 1, 1, 0, 0, 0).strftime('%s'))
END_TIME = int(datetime.datetime(int(year), 12, 31, 23, 0, 0).strftime('%s'))

# Use RegEx to filter out by year
regex = re.compile(year)

# Get all events from our Holidays Calendar
events = nylas.events.where(calendar_id=os.environ.get("HOLIDAYS_CALENDAR_ID"), 
starts_after = START_TIME, ends_before = END_TIME)

# Store all events in an array
for event in events:
    yearly_events.append(event.when["date"] + " " + event.title)

# Use our RegEx to get only events happening on the requested year
#filtered_events = [event for event in yearly_events if regex.match(event)]
filtered_events = yearly_events

# Open the file to write to it
with open(file_name, "w") as f:
    f.write(f"**Holidays Calendar {year}**\n\n")
    # Loop months to get events for each of them
    for month in months:
        if month < 10:
            s_month = "0" + str(month)
        else:
            s_month = str(month)
        # Get only the current month using RegEx
        regex = re.compile(".*-" + s_month + "-.*")
        filtered_month = [event for event in filtered_events if regex.match(event)]
        # Print name of month
        f.write(f"|{calendar.month_name[month]}| |")
        f.write("\n|-|-|")
        # If we have events for that month, print them out
        if len(filtered_month) > 0:
            for filtered in filtered_month:
                # We want to get the day the event happens
                day = re.findall("(..\s)", filtered)
                # And we want the event title
                event = re.findall("(\s.*)", filtered)
                f.write(f"\n|{day[0]}|{event[0]}|")
            f.write("\n\n")
        else:
            f.write("\n\n")
# Close the file
f.close()

The code is doing the following items:

  1. We are getting the first and last day of the year for the parameter that we will pass to our script.
  2. Then, we will get all events for that time range for our company holidays calendar.
  3. Once we have them, we want to extract the date and title.
  4. Finally, we will create a markdown file with the information we have.

Step 3 – Creating a Holiday Calendar

To create a holiday calendar, we need to run our script passing the year that we want to process:

$ python3 Holiday_Calendar.py 2023

This will create the Holiday Calendar 2023.md file on our Obsidian installation.

Step 4 – Visualizing our Calendar

If we open Obsidian, we will find the newly created note displaying the holidays for the specified year.

Holiday Calendar

What’s next?

To learn more about Calendars and Events, check our official documentation on Calendars and Events.

Why don’t you sign up Nylas for free and start building!

Don’t miss the action, watch our LiveStream Coding with Nylas:

Related resources

How to Send Emails Using an API

Key Takeaways This post will provide a complete walkthrough for integrating an email API focused…

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.