![]() |
Python offers a built-in module called calendar
that lets you work with calendars easily and effectively. In this article, we’ll explore three different scripts to generate text-based calendars directly from your terminal or CLI app.
🗓️ 1. Single-Month Calendar
import calendar
year = 2025
month = 7
print(calendar.month(year, month))
This script prints the calendar of a single month. Change the year
and month
values to display any month you like.
🖨️ Output
July 2025
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
📆 2. Full-Year Calendar
import calendar
year = 2025
print(calendar.calendar(year))
This simple script prints the full calendar for the specified year, month by month.
🖨️ Output (beginning of the year)
2025
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
(continues with April–December)
🌟 3. Calendar with Highlighted Current Day
import calendar
from datetime import datetime
def print_calendar_with_highlight(year, month):
today = datetime.today()
current_day = today.day if today.year == year and today.month == month else -1
cal = calendar.TextCalendar(calendar.MONDAY)
cal_str = cal.formatmonth(year, month)
HIGHLIGHT_START = "\033[96m"
HIGHLIGHT_END = "\033[0m"
lines = cal_str.splitlines()
highlighted_lines = []
for line in lines:
if current_day != -1:
day_str = str(current_day)
if ' ' + day_str + ' ' in line:
line = line.replace(' ' + day_str + ' ', HIGHLIGHT_START + '|' + day_str + '|' + HIGHLIGHT_END)
highlighted_lines.append(line)
print("\n".join(highlighted_lines))
# Usage
current_year = datetime.today().year
current_month = datetime.today().month
print_calendar_with_highlight(current_year, current_month)
🔍 Detailed Explanation
- Imports: The
calendar
module (to create calendars) anddatetime
(to get the current date) are imported. print_calendar_with_highlight
function: Accepts a year and month, prints the calendar for that month, and highlights today if the date matches.- Detect current day: Uses
datetime.today()
to fetch today’s date and compare it with the input. If they match, it stores the day number. - Calendar generation:
TextCalendar
creates a text-based calendar, starting on Monday. It’s converted into lines usingsplitlines()
. - ANSI Highlighting: If the current day is found in a line, it replaces it with a highlighted version using ANSI codes (cyan in this case).
\033[96m
starts color, and\033[0m
resets it. - Final print: Modified lines are printed. The current day appears highlighted in supported terminals.
🖨️ Output (example for July 9, 2025)
July 2025 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 |9|10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Note: Highlighting only works on terminals that support ANSI escape codes (Linux/macOS, Windows PowerShell, etc.).
✅ Conclusion
These three scripts demonstrate how powerful yet simple Python can be for handling calendar tasks. Whether you need a monthly snapshot, a full year, or dynamic highlights — the calendar
module is a great built-in tool with no external libraries required.
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment