Google Calendar Event Adder#
This Python script allows you to bulk-add events to your Google Calendar by reading event data from a JSON file. It utilizes the Google Calendar API v3 and Pydantic for data validation.
Features#
- Adds multiple events from a structured JSON file.
- Supports specifying start/end times or creating all-day events.
- Allows setting event summaries, descriptions, and locations.
- Configurable reminders (popup or email) for each event.
- Uses OAuth 2.0 for secure authentication with Google Calendar.
- Validates the input JSON format using Pydantic.
- Flexible command-line arguments for specifying input file, credentials, and target calendar.
Prerequisites#
- Python 3: The script is written for Python 3. Ensure you have it installed. The script includes a shebang
#!/usr/bin/env python3for direct execution on Unix-like systems. - Google Account: You need a Google account to access Google Calendar.
- Google Cloud Project & API Credentials:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Enable the Google Calendar API for your project.
- Create OAuth 2.0 Client ID credentials:
- Select Desktop app as the application type.
- Give it a name (e.g., “Calendar Script”).
- Download the credentials JSON file. Rename it to
credentials.json(or use a different name and specify it with the--credentialsflag).
Setup#
Clone the repository (or download the script):
git clone https://github.com/tashifkhan/Calender-Event-Adding-Script/ cd Calender-Event-Adding-ScriptOr simply save the script code as a
.pyfile (e.g.,add_calendar_events.py).Place Credentials File: Put the downloaded
credentials.jsonfile in the same directory as the script, or note its path to use with the--credentialsflag.Install Dependencies: Create a
requirements.txtfile with the following content:google-api-python-client google-auth-httplib2 google-auth-oauthlib pydanticThen, install the required packages using pip:
pip install -r requirements.txt(Optional) Make the script executable (macOS/Linux):
chmod +x add_calendar_events.py
JSON Input Format#
The script expects a JSON file containing a single top-level key "events" which holds a list of event objects. Each event object should have the following structure:
{
"events": [
{
"date": "YYYY-MM-DD", # Required: Event date
"start_time": "HH:MM", # Optional: Start time (24-hour format). If omitted (with end_time), creates an all-day event.
"end_time": "HH:MM", # Optional: End time (24-hour format). Required if start_time is present.
"summary": "Event Title", # Required: The main title/summary of the event.
"description": "More details...", # Optional: A longer description for the event.
"location": "Event Location", # Optional: Physical location or meeting link.
"reminders": { # Optional: Reminder settings. If omitted, calendar defaults are used.
"method": "popup" or "email", # Required if reminders object is present: Type of reminder.
"minutes": 30 # Required if reminders object is present: Minutes before event to remind.
}
},
{
"date": "2025-05-05",
"summary": "Project Deadline", # Example of an all-day event (no start/end time)
"description": "Submit final deliverables",
"reminders": {
"method": "popup",
"minutes": 120
}
}
# ... more event objects
]
}
Example JSON (events.json):
{
"events": [
{
"date": "2025-04-20",
"start_time": "10:00",
"end_time": "11:00",
"summary": "Meeting with Team",
"description": "Discuss project progress",
"location": "Conference Room A",
"reminders": {
"method": "popup",
"minutes": 30
}
},
{
"date": "2025-05-01",
"start_time": "14:00",
"end_time": "15:30",
"summary": "Client Presentation",
"description": "Present the new design",
"location": "Client's Office",
"reminders": {
"method": "email",
"minutes": 60
}
},
{
"date": "2025-05-05",
"summary": "Project Deadline",
"description": "Submit final deliverables",
"reminders": {
"method": "popup",
"minutes": 120
}
}
]
}
Usage#
Run the script from your terminal, providing the path to your JSON event file.
Basic Usage (using defaults):
./add_calendar_events.py path/to/your/events.json
(Assumes credentials.json is in the same directory and adds to the primary calendar)
Specifying Credentials File:
./add_calendar_events.py path/to/your/events.json --credentials /path/to/your/client_secrets.json
Specifying Target Calendar ID:
./add_calendar_events.py path/to/your/events.json --calendar-id [email protected]
(Replace your_calendar_id@... with the actual ID found in your Google Calendar settings)
Using All Options:
./add_calendar_events.py events.json --credentials client_secrets.json --calendar-id [email protected]
Command-Line Arguments:
json_file_path(Required): Positional argument for the path to the input JSON file.--credentials(Optional): Path to the Google API credentials file. Defaults tocredentials.jsonin the script’s directory.--calendar-id(Optional): The ID of the calendar to add events to. Defaults toprimary(the user’s main calendar).
Authentication Flow#
- First Run: When you run the script for the first time, it will open a web browser window asking you to log in to your Google Account and grant the script permission to manage your calendar events.
token.json: After successful authorization, the script creates atoken.jsonfile in the same directory. This file stores your access and refresh tokens, so you don’t have to re-authorize every time.- Security: Do NOT share your
credentials.jsonortoken.jsonfiles. Addtoken.jsonto your.gitignorefile if using Git. - Token Refresh: The script attempts to refresh the access token if it expires. If refreshing fails (e.g., permissions revoked), it will delete the
token.jsonfile and prompt you to re-authenticate.
Dependencies#
The required Python packages are listed in requirements.txt:
google-api-python-client: Google API Client Library for Python.google-auth-httplib2: Google Authentication Library for Python with httplib2.google-auth-oauthlib: Google Authentication Library for Python using OAuthlib.pydantic: Data validation and settings management using Python type annotations.
Contributing#
Feel free to open issues or submit pull requests if you find bugs or have suggestions for improvements.
License#
MIT LICENSE