Thursday, 20 November 2025

2026 calendar with Calendar Week (CW) in Python



Quick summary: this short script prints a neat month-by-month calendar for a given year with ISO calendar week (CW) numbers in the left column. It uses Python’s built-in calendar and datetime modules, formats weeks so Monday is the first day, and prints each week with its ISO week number.


Why this is useful

ISO calendar week numbers are commonly used in project planning, timesheets, and logistics. Having a compact textual calendar that shows the CW next to every week makes it easy to align dates to planning periods.


The code (conceptual outline)

Below I explain the main parts of the script step by step. The screenshot of the original code / output is included at the end.

1) Imports and year variable

import calendar import datetime clcoding = 2026 # Year
  • calendar provides utilities to generate month layouts (weeks → days).

  • datetime gives date objects used to compute the ISO week number.

  • clcoding is just the chosen variable that holds the year to print — change it to print a different year.

2) Make Monday the first weekday

calendar.setfirstweekday(calendar.MONDAY)
  • calendar.monthcalendar() respects whatever first weekday you set. For ISO weeks, Monday-first is the standard, so we set it explicitly.

  • Note: ISO week numbers returned by date.isocalendar() also assume Monday-first; these two choices are consistent.

3) Iterate over months

for month in range(1, 13): print(f"{calendar.month_name[month]} {clcoding}".center(28)) print("CW Mon Tue Wed Thu Fri Sat Sun")
  • Loop months 1..12.

  • Print the month heading and a header row that shows CW plus Monday→Sunday day names.

  • .center(28) centers the header in a ~28-character width for nicer output.

4) Build the month grid and compute CW for each week

for week in calendar.monthcalendar(clcoding, month): # Find the first valid date in the week to get the CW first_day = next((d for d in week if d != 0), None) cw = datetime.date(clcoding, month, first_day).isocalendar()[1] days = " ".join(f"{d:>3}" if d != 0 else " " for d in week) print(f"{cw:>3} {days}") print()

Explain each piece:

  • calendar.monthcalendar(year, month) returns a list of weeks; each week is a list of 7 integers. Days that fall outside the month are 0. Example week: [0, 0, 1, 2, 3, 4, 5].

  • first_day = next((d for d in week if d != 0), None) picks the first non-zero day in the week. We use that date to determine the ISO week number for the row.

    • That is: if a week contains at least one day of the current month, the week’s week-number is the ISO week of that first valid day.

  • datetime.date(year, month, first_day).isocalendar()[1]:

    • .isocalendar() returns a tuple (ISO_year, ISO_week_number, ISO_weekday).

    • We take [1] to get the ISO week number (CW).

    • Important subtlety: ISO week numbers can assign early Jan days to week 52/53 of the previous ISO year and late Dec days to week 1 of the next ISO year. Using isocalendar() on an actual date handles that correctly.

  • days = " ".join(f"{d:>3}" if d != 0 else " " for d in week):

    • Formats each day as a 3-character right-aligned field; zeros become three spaces so columns line up.

  • print(f"{cw:>3} {days}") prints the CW (right aligned in 3 spaces) and then the seven day columns.


Edge cases & notes

  • first_day will always be non-None for monthcalendar weeks because each week row returned by monthcalendar covers the month and will include at least one day != 0. (So the None fallback is defensive.)

  • ISO week numbers can be 52 or 53 for adjacent years. If you need to show the ISO year too (e.g., when the week belongs to the previous/next ISO year), use .isocalendar()[0] to get that ISO year.

  • calendar.setfirstweekday() only affects the layout (which weekday is the first column). ISO week semantics assume Monday-first — so they match here.

  • To start weeks on Sunday instead, set calendar.SUNDAY, but ISO week numbers will then visually mismatch ISO semantics (ISO weeks still mean Monday-based weeks), so be careful.


Quick customization ideas

  • Show ISO year with week: iso_year, iso_week, _ = datetime.date(...).isocalendar() and print f"{iso_year}-{iso_week}".

  • Save to file instead of printing: collect lines into a string and write to a .txt or .md.

  • Different layout: use calendar.TextCalendar and override formatting if you want prettier text blocks.

  • Highlight current date: compare with datetime.date.today() and add a * or color (if terminal supports it).


Example output

The script produces month blocks like this (excerpt):

January 2026 CW Mon Tue Wed Thu Fri Sat Sun 1 1 2 3 4 2 5 6 7 8 9 10 11 3 12 13 14 15 16 17 18 4 19 20 21 22 23 24 25 5 26 27 28 29 30 31

(Each month prints similarly with its calendar week numbers aligned left.)


Final thoughts

This is a compact, readable way to generate a full-year calendar annotated with ISO calendar week numbers using only Python’s standard library. It’s easy to adapt for custom output formats, exporting, or including ISO year information when weeks spill across year boundaries

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (161) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (254) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (225) Data Strucures (14) Deep Learning (75) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (48) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (197) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (12) PHP (20) Projects (32) Python (1219) Python Coding Challenge (898) Python Quiz (348) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)