Create ICS Calendar Invites in Python

This script generates a batch of calendar invites in the ICS format using the ics library.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 11 views 0 copies

Requires third-party packages — install first
pip install ics

Python code

23 lines
Python 3.9+
import 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

stdout
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

  1. Use `ics.Event` with `duration` instead of `end`
  2. 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

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.