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
-
calendarprovides utilities to generate month layouts (weeks → days). -
datetimegives date objects used to compute the ISO week number. -
clcodingis just the chosen variable that holds the year to print — change it to print a different year.
2) Make Monday the first weekday
-
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
-
Loop months 1..12.
-
Print the month heading and a header row that shows
CWplus 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
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 are0. 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_daywill always be non-Nonefor monthcalendar weeks because each week row returned bymonthcalendarcovers the month and will include at least one day != 0. (So theNonefallback 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 printf"{iso_year}-{iso_week}". -
Save to file instead of printing: collect lines into a string and write to a
.txtor.md. -
Different layout: use
calendar.TextCalendarand 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):
(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
%20in%20Python.png)

0 Comments:
Post a Comment