Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24376

Real Python: The Python calendar Module: Create Calendars With Python

$
0
0

The Python calendar module provides several ways to generate calendars for Python programs. It also includes a variety of functions for working with calendar data as strings, numbers, and datetime objects.

In this tutorial, you’ll learn how to use the calendar module to create and customize calendars with Python.

By the end of this tutorial, you’ll be able to:

  • Display calendars in your terminal with Python
  • Create plain text and HTML calendars
  • Format calendars for specific locales and display conventions
  • Use calendar-related functions and methods to access lower-level calendar data in a variety of formats

Get Your Code:Click here to download the free sample code you’ll use to learn about creating calendars with the calendar module in Python.

Take the Quiz: Test your knowledge with our interactive “The Python calendar Module” quiz. You’ll receive a score upon completion to help you track your learning progress:


The Python calendar Module: Create Calendars With Python

Interactive Quiz

The Python calendar Module

In this quiz, you'll test your understanding of the calendar module in Python. It'll evaluate your proficiency in manipulating, customizing, and displaying calendars directly within your terminal. By working through this quiz, you'll revisit the fundamental functions and methods provided by the calendar module.

Displaying Calendars in Your Terminal

Unix and Unix-like operating systems such as macOS and Linux include a cal command-line utility for displaying calendars in an interactive console:

Shell
$ cal
      May 2024        Su Mo Tu We Th Fr Sa            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     
Copied!

Python provides a similar tool, which allows you to run the calendar module as a command-line script. To begin exploring the Python calendar module, open up your terminal program and enter the following command:

Shell
$ python-mcalendar
                                  2024      January                   February                   MarchMo 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  6  7                1  2  3  4                   1  2  3 8  9 10 11 12 13 14       5  6  7  8  9 10 11       4  5  6  7  8  9 1015 16 17 18 19 20 21      12 13 14 15 16 17 18      11 12 13 14 15 16 1722 23 24 25 26 27 28      19 20 21 22 23 24 25      18 19 20 21 22 23 2429 30 31                  26 27 28 29               25 26 27 28 29 30 31       April                      May                       JuneMo 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  6  7             1  2  3  4  5                      1  2 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  915 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 1622 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 2329 30                     27 28 29 30 31            24 25 26 27 28 29 30        July                     August                  SeptemberMo 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  6  7                1  2  3  4                         1 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  815 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 1522 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 2229 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29                                                    30      October                   November                  DecemberMo 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  6                   1  2  3                         1 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  814 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 1521 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 2228 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29                                                    30 31
Copied!

Running python -m calendar with no arguments outputs a full year’s calendar for the current year. To display the full calendar for a different year, pass in the integer representation of a year as the first argument of the calendar command:

Shell
$ python-mcalendar1989
Copied!

To view a single month, pass in both a year and a month as the second parameter:

Shell
$ python-mcalendar205407     July 2054Mo Tu We Th Fr Sa Su       1  2  3  4  5 6  7  8  9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31
Copied!

As you can see in these examples, the calendar module can display calendars for both past and future dates. According to the official documentation, the calendar module uses the current Gregorian calendar, extended indefinitely in both directions. It also uses the ISO 8601 standard, which is an international standard for exchanging and communicating date and time-related data.

Now that you know how to display calendars in your terminal with Python, you can move on and explore other approaches to creating calendars as plain text or HTML markup representations.

Creating Text-Based Calendars

To generate plain text calendars, the calendar module provides calendar.TextCalendar with methods to format and print monthly and yearly calendars.

TextCalendar.formatyear() accepts a single parameter for the year, like the calendar command-line script. Try it out in your Python REPL by executing the following code:

Python
>>> importcalendar>>> text_calendar=calendar.TextCalendar()>>> text_calendar.formatyear(2024)'                                  2024\n\n      January    (...)'
Copied!

Read the full article at https://realpython.com/python-calendar-module/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 24376

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>