Create ICS Calendar Invites in Python
This script generates a batch of calendar invites in the ICS format using the ics library.
Requires third-party packages — install first
pip install ics
Python code
23 linesimport ics
from datetime import datetime, timedelta
def create_invites(batch):
calendar = ics.Calendar()
for event_data in batch:
event = ics.Event()
event.name = event_data["name"]
event.begin = event_data["start"]
event.end = event_data["end"]
event.description = event_data.get("description", "")
calendar.events.add(event)
with open("invites.ics", "w") as f:
f.writelines(calendar)
if __name__ == "__main__":
batch = [
{"name": "Team Meeting", "start": datetime(2024, 1, 15, 10, 0), "end": datetime(2024, 1, 15, 11, 0), "description": "Weekly sync"},
{"name": "Lunch with Client", "start": datetime(2024, 1, 16, 12, 30), "end": datetime(2024, 1, 16, 13, 30)},
]
create_invites(batch)
print("Batch invites created in invites.ics")
Output
Batch invites created in invites.ics
How it works
The ics library provides a high-level API for creating iCalendar events. By creating an ics.Calendar object and adding ics.Event instances, you can build a valid ICS file. Each event requires a name and start/end times; descriptions are optional. The writelines method serializes the calendar to the file. This pattern is useful for automating event scheduling tasks.
Common mistakes
- Using `datetime` from the wrong module (e.g., `datetime.datetime` instead of `datetime`)
- Not handling timezone-aware datetimes correctly
- Forgetting to import the `ics` package before use
Variations
- Use `ics.Event` with `duration` instead of `end`
- Add attendees or reminders using event properties
Real-world use cases
- Automating meeting invites for weekly team syncs from a database or CSV.
- Generating calendar events for event registrations or customer booking reminders.
- Creating holiday schedules or training session calendars to send to multiple participants.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.