![Create Calendars With Python – Real Python Create Calendars With Python – Real Python](https://techtoguide.com/wp-content/uploads/2024/05/Create-Calendars-With-Python-–-Real-Python-768x432.jpg)
[ad_1]
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
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:
Interactive Quiz
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:
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:
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:
To view a single month, pass in both a year and a month as the second parameter:
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:
Here, after instantiating a text_calendar
instance of calendar.TextCalendar
, you call .formatyear(2024)
, which returns the full calendar for 2024 as a multiline string. You can make that look nicer with TextCalendar.pryear()
, which prints the year’s calendar in a more readable format:
As you can see, .pryear()
requires the year as its first argument. It also provides some optional parameters to format the calendar’s display properties that look like this: .pryear(theyear, w=2, l=1, c=6, m=3)
.
The w
parameter sets the date column width, l
is the number of lines per week, c
sets the spacing between month columns, and m
is the number of month columns.
To format and print a calendar for a single month as plain text, TextCalendar
provides .formatmonth()
and .prmonth()
, which are similar to the yearly calendar methods. The .prmonth()
method includes a second required parameter for the month as an integer, as well as two optional parameters: w
for the width of the date columns, and l
for the number of lines each week will use. Both of these optional parameters default to zero.
Working in the same console session as before, use TextCalendar.prmonth()
to generate and print a plain text calendar for a single month like so:
Now that you understand the basics of creating calendars in plain text, you can take it a step further and learn how to generate HTML calendars with Python to display on the web.
Generating and Styling HTML Calendars
If you need to create a calendar for a website, the calendar
module can also output calendars in HTML with calendar.HTMLCalendar
. Its methods .formatmonth()
and .formatyear()
work like their corresponding TextCalendar
methods. But they replace the formatting parameters with the ability to generate CSS classes for styling HTML.
To use this class, create an html_calendar
instance in your Python console and test it out by calling .formatyear()
:
This will return a long Python string of HTML markup representing a calendar for the full year as an HTML table. To view this HTML calendar in a web browser, first use Python’s print()
function to output the string as HTML in your console:
Now copy the output of that command and paste it into a new file called formatyear.html
. Open that file with your computer’s web browser, and it should appear something like this:
![Calendar as un-styled HTML table](https://files.realpython.com/media/python-calendar-html-year.0bc3c94049dc.png)
As you can see, unstyled HTML tables are not exactly the most user-friendly way to display and read a calendar. Thankfully, HTMLCalendar.formatyearpage()
allows you to include a CSS stylesheet by generating a year’s calendar as a full HTML page. This method’s optional css
parameter defaults to a value of 'calendar.css'
, referring to the name of the CSS file that will include the custom styles.
To create a new file containing a full HTML page generated by .formatyearpage()
, use Python’s built-in open()
function. Then pass in 'wb'
as the second argument after the file name to write to the file in binary mode:
Here you use the Python with
statement to open your file, creating a context manager object to ensure the file will always be closed even if an exception is thrown in the with
block. It’s important to always close files opened by Python to preserve operating system resources and protect the files from data loss.
Now you should see a new file called formatyearpage.html
in the folder where you opened your Python console. Open that file, and you’ll see the markup for a full HTML page including this <link>
tag:
Create a new file called calendar.css
in the same directory as the formatyearpage.html
file and add the following CSS code:
Now you can open formatyearpage.html
with your web browser to see how you’ve made your calendar a bit more readable. You’ve added some padding around the date rows and columns, increased the margins of the month tables, and updated the HTML body’s font property:
![HTML calendar with custom CSS styles](https://files.realpython.com/media/python-calendar-html-year-page.a513a5717133.png)
In addition to the methods you’ve learned to create plain text and HTML calendars, the calendar
module also allows you to create calendars for different global locations, which you’ll explore next.
Formatting Calendars for Specific Locations
By default, all calendars generated by the calendar
module follow the European convention of using Monday as the first day of the week. You can change this with Calendar.setfirstweekday()
, which is available as an instance method for TextCalendar
and HTMLCalendar
.
The calendar
module uses integers to represent days of the week, where zero is Monday, and six is Sunday, or it uses constants such as calendar.MONDAY
, which is defined for readability.
To change the first day of the week from Monday to Sunday for a plain text calendar, execute the following code in your Python console:
Additionally, the calendar
module provides location-specific subclasses of TextCalendar
and HTMLCalendar
that display the month and weekday names for a specific locale. Below is an example of how it would look if you used calendar.LocaleTextCalendar
to output a calendar for the locale of Germany using UTF-8 encoding:
Your calendar now displays the month and day of the week abbreviations based on their German spelling.
Note: Locale codes vary by operating system, as explained in the Python locale
module’s documentation.
Working With Other Python Calendar Functions and Methods
There are several additional methods available on all Calendar
and subclass instances that allow you to work with lower-level calendar data and prepare that data for formatting. For simplicity’s sake, you’ll explore a few of them here using TextCalendar
.
TextCalendar.iterweekdays()
returns an iterator of weekday number values for a calendar, beginning with the day set by .setfirstweekday()
for the calendar:
In this code, the call to text_calendar.iterweekdays()
returns a generator object that you can then loop through to display each of the weekday numerical values for your calendar. If you were to change the first weekday to Sunday, it would have the following effect on the output of this method:
TextCalendar.itermonthdates()
returns an iterator for all the days in a given month as Python datetime.date
objects, including the days before the start of the month, and the days after the end of the month that are needed to complete those weeks.
Here’s an example of how you loop through the iterator and use datetime.date.strftime()
to print a readable version of each datetime.date
object:
There are several methods similar to TextCalendar.itermonthdates()
including .itermonthdays()
, which return the days of the month as numbers, with zeros for the days needed to complete the first and last week of the month:
In this output, the zeros at the beginning and end indicate the days in December and February that need to be included in the visual representation of the calendar to complete the first and last weeks of January.
Similarly, .itermonthdays2()
returns a tuple for each day containing the day of the month and weekday number. Additional methods include the year, day of the week, and day of the month as tuples:
Here you can see the zero dates for December and February along with the numbered days of the week that they represent in the calendar as the second item of the tuples.
The calendar
module also includes a couple of module-level functions for working specifically with leap years. The .isleap()
function
determines if the given year is a leap year, and .leapdays()
accepts two years as arguments, identifying the number of leap years between the two years:
These are just a few of the lower-level functions and methods that the calendar
module provides for working with calendar-related data. In the next section, you’ll explore some ideas to go further in your learning and start using Python’s calendar-building capabilities in your own projects.
Next Steps
Now that you’ve got a sense of the core features of the Python calendar
module, check out the official documentation to dig in a bit deeper into the additional features the module has to offer.
For example, calendar.HTMLCalendar
provides several attributes for overriding and customizing the default CSS classes for more fine-grained control over your calendar’s appearance. And with the ability to subclass and extend HTMLCalendar
itself, there’s a wide range of possibilities for building interactive web-based calendars with Python by overwriting its attributes and methods.
In addition to the variety of tools you’ve explored so far, the calendar.Calendar
base class provides many other methods for working with calendar data. These methods don’t return fully formatted calendars. Rather, they provide the pieces of data that are necessary to compose those calendars. So you can use them to prepare data for formatting in more complex or interactive calendar applications.
To complement your mastery of the Python calendar
module, take some time to dive into the details of the Python datetime
and time
modules that can be combined to build richly featured programs that work with dates and times.
Conclusion
Calendars are a key component of many software programs and websites. With the Python calendar
module, there are a variety of options available for you to create, display, and customize calendars in your Python programs.
In this tutorial, you’ve learned:
- How to display calendars interactively by running the
calendar
module as a command-line script in your terminal. - How to generate plain text and HTML calendars and use the various formatting and styling options for each type
- How to format calendars for specific locations and display conventions
- How to use lower-level functions and methods for working with calendar data
Now that you’re familiar with the range of functionality the calendar
module provides, it’s time to put that knowledge to use as you build your own calendar applications with 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:
Interactive Quiz
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.
Copied!
Happy Pythoning!
[ad_2]