Wednesday, April 17, 2024

Creating iCalender files (Events)

In my previous blog, I shared how to read an existing event file in iCalendar format (commonly with ICS extension). Be sure to read that before continuing.

Here, I’ll show you how to create one from scratch.

Let’s support we want an event that is:
starting on feb 09, 2023 at 1pm and ending at 2pm same day with description, summary, and location.
AND we want it to repeat monthly.

The output (.ics)/iCalendar file content we want from this code to be saved should look like this:
DTSTAMP:60 min Massage Session
LOCATION: 15111 Main Street, Home City, WA 99999
DESCRIPTION:60 min massage session with Jen at 1:00 pm (contact: 555-555-5555).\nPlease plan to arrive 10-15 minutes prior to your appointment time.\nSee you then!
SUMMARY: 60 min massage session – Home City

As in the previously shared code, we will need icalendar library and optionally I’ll also use datetime module for making dates more user-friendly. The complete code is below:

creating an ICS file

After running it, open the ics file using any text editor to inspect its content. You can also just double-click it to open the file and the calendar application associated with calendar objects will automatically open it (e.g. in Windows it may be Mail program or Outlook, in MacOS and Linux it’ll be different apps).

For example, here’s how this exact file would show in Outlook of Office 365 subscription version:

our ICS file opened in Outlook

Notice how the Recurrence button is depressed in the UI. It already detected that we created the event with recurrence.

If we now click on Recurrence button, we can get details of the recurrence as shown below:

recurrence details

We can see that it’s recurrence pattern is set to Monthly, exactly as we coded it. Perfect!


Now that we know how to create an event in iCalendar format, open an existing one and extract specific information from it, how about we complete the whole workflow by also opening the even file from within Python code? Below, I’ll share the platform-independent code to do just that.

Because each operating system runs its own flavor of mail/calendar client application and also the way they’re invoked are different, we have to do some pre-check on which operating system we’re working since we want it platform-independent. For that, we’ll leverage os and subprocess modules.

launching an event

If we run this program assuming we have a test.ics event file in the current directory, in Windows OS the Outlook application (if Office is installed, otherwise default/free Mail program will open which is also fine) a window will pop up with the event information neatly organized and displayed in a user-friendly form. If you wanted to insert that event into your calendar, simply click Save & Close button on the dialog (depending on the client application you’re using, the button or menu command to save may be slightly different).

I hope you enjoyed this tutorial.

Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top