How to Build a Colorful Calendar Using Python

As a Python enthusiast, I love creating visually appealing and functional tools using different libraries. One of my favorite projects is generating a colorful calendar using the rich library. This powerful library allows formatting tables with colors and styles, making the terminal output look more engaging.

I’ll walk you through how to create a fully functional, interactive, and visually appealing calendar using Python. I’ll also share an enhanced version that includes holiday highlights, user input for year selection, and automatic detection of today’s date.

Breaking Down the Code

Imports Required Modules

To start, we need to import the necessary modules:

import calendar
from rich.console import Console
from rich.table import Table
  • calendar: Python’s built-in module to work with dates and calendars.
  • Console: Part of the rich library, used to display rich text in the terminal.
  • Table: Also from rich, helps create structured and styled tables.

Function Definition

def colorful_calendar(year):

This function takes a year as input and prints a colorful calendar.

Creating a Console Object

console = Console()

This initializes a console instance to print the calendar in a structured way.

Generating Month Data

months = [calendar.monthcalendar(year, m) for m in range(1, 13)]
  • calendar.monthcalendar(year, m): Returns a matrix (list of weeks) where each week is represented by a list of seven numbers (days of the week). Days outside the month are set as 0.
  • This generates a list of 12 months with their corresponding weeks and days.

Looping Through Months

for month in range(12):
  • Iterates through the months from January (0) to December (11).

Fetching Month Name

month_name = calendar.month_name[month + 1]
  • calendar.month_name is a list where index 1 = “January”, 2 = “February”, etc.
  • We use month + 1 since range(12) starts at 0.

Creating a Table

table = Table(title=f"[bold cyan]{month_name} {year}[/bold cyan]", show_lines=True)
  • A table is created with the month name and year as the title.
  • show_lines=True ensures table borders are visible.

Adding Columns for Days of the Week

table.add_column("Mon", justify="center", style="green")
table.add_column("Tue", justify="center", style="green")
table.add_column("Wed", justify="center", style="green")
table.add_column("Thu", justify="center", style="green")
table.add_column("Fri", justify="center", style="green")
table.add_column("Sat", justify="center", style="green")
table.add_column("Sun", justify="center", style="red")
  • Each column represents a day of the week.
  • Weekdays are green; Sundays are red.

Filling the Table with Days

for week in months[month]:
    table.add_row(*[str(day) if day != 0 else "" for day in week])
  • Iterates over the weeks of the month.
  • Days are added as table rows.
  • Empty strings replace 0s (days that do not belong to the month).

Printing the Table

console.print(table)
console.print("\n")
  • Displays the formatted table with spacing.

Calling the Function

colorful_calendar(2025)
  • Generates and prints the calendar for 2025.

Enhancements & Added Practical Functionality

To make this calendar more practical, we can:

  1. Highlight today’s date if displaying the current year.
  2. Allow user input for year selection.
  3. Show special event days (holidays, weekends, etc.).
  4. Make it interactive using user choice.

Updated Code with More Functionality

import calendar
import datetime
from rich.console import Console
from rich.table import Table

def colorful_calendar(year, highlight_today=True, show_holidays=False):
    console = Console()
    months = [calendar.monthcalendar(year, m) for m in range(1, 13)]
    today = datetime.date.today()
    holidays = {1: [1], 12: [25]}

    for month in range(12):
        month_name = calendar.month_name[month + 1]
        table = Table(title=f"[bold cyan]{month_name} {year}[/bold cyan]", show_lines=True)
        table.add_column("Mon", justify="center", style="green")
        table.add_column("Tue", justify="center", style="green")
        table.add_column("Wed", justify="center", style="green")
        table.add_column("Thu", justify="center", style="green")
        table.add_column("Fri", justify="center", style="green")
        table.add_column("Sat", justify="center", style="yellow")
        table.add_column("Sun", justify="center", style="red")
        
        for week in months[month]:
            row = []
            for day in week:
                if day == 0:
                    row.append("")
                elif highlight_today and day == today.day and month + 1 == today.month and year == today.year:
                    row.append(f"[bold reverse yellow]{day}[/bold reverse yellow]")
                elif show_holidays and month + 1 in holidays and day in holidays[month + 1]:
                    row.append(f"[bold red]{day}[/bold red]")
                else:
                    row.append(str(day))
            table.add_row(*row)
        console.print(table)
        console.print("\n")

# User input
year_input = int(input("Enter the year for the calendar: "))
show_today = input("Highlight today’s date? (yes/no): ").strip().lower() == "yes"
show_holiday_input = input("Show holiday highlights? (yes/no): ").strip().lower() == "yes"

colorful_calendar(year_input, highlight_today=show_today, show_holidays=show_holiday_input)

Conclusion

This improved script makes the calendar interactive, practical, and visually appealing! It allows user input, highlights today’s date, and marks holidays.

Related blog posts