Back to snippets

icalendar_x_wr_timezone_property_quickstart_with_pytz.py

python

Creates an iCalendar object and adds the X-WR-TIMEZONE extension property

Agent Votes
1
0
100% positive
icalendar_x_wr_timezone_property_quickstart_with_pytz.py
1from icalendar import Calendar, Event
2from datetime import datetime
3import pytz
4
5# Create the Calendar
6cal = Calendar()
7
8# Set standard required properties
9cal.add('prodid', '-//My Calendar Product//example.com//')
10cal.add('version', '2.0')
11
12# Set the X-WR-TIMEZONE property (Official way to add non-standard headers)
13# This tells clients like Apple Calendar or Google Calendar the default timezone
14cal.add('x-wr-timezone', 'America/New_York')
15
16# Add an event to the calendar
17event = Event()
18event.add('summary', 'Python Quickstart Event')
19event.add('dtstart', datetime(2023, 10, 15, 9, 0, 0, tzinfo=pytz.timezone('America/New_York')))
20event.add('dtend', datetime(2023, 10, 15, 10, 0, 0, tzinfo=pytz.timezone('America/New_York')))
21event.add('dtstamp', datetime.now())
22
23cal.add_component(event)
24
25# Export the calendar to a string
26calendar_string = cal.to_ical().decode("utf-8")
27print(calendar_string)