Create Google Calendar Events From Excel File

This project came about because I like to create project plans starting in Excel. I was curious to see if I could put meetings on my Google Calendar. A direct link to the notebook can be found on my github.

Google Developer Console Setup

  1. Go to Google Developer Console
  2. Click ‘ENABLE APIS AND SERVICES’
  3. Search ‘calendar’ and select ‘Google Calendar API’
  4. Enable the API
  5. Create a new project and name your project
  6. Click on OAuth consent screen and select ‘External’
  7. At the next screen name the Application and then hit save
  8. Click on Credentials and then click ‘CREATE CREDENTIALS’
  9. Select OAuth Client ID
  10. Select other and hit create. Hit ok at the next screen that pops up
  11. You are now setup with google and should download the json file under OAuth 2.0
    Client IDs. This is your client secret file, which in the code below is named
    ‘client_secret_calendar.json’

Python Code
Import & Setup

from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from httplib2 import Http

scopes = ['https://www.googleapis.com/auth/calendar']
flow = InstalledAppFlow.from_client_secrets_file("client_secret_calendar.json", scopes=scopes)
credentials = flow.run_console()

#log in and copy credentials
import pickle
pickle.dump(credentials, open("token.pkl", "wb"))

credentials = pickle.load(open("token.pkl", "rb"))
service = build("calendar", "v3", credentials=credentials,cache_discovery=False)

result = service.calendarList().list().execute()
result = service.events().list(calendarId=calendar_id).execute()

print(result['items'][0])

# The calendar you want to update may be a different index
calendar_id = result['items'][1]['id']

Read In Excel Source File

import pandas as pd
from datetime import datetime, timedelta

file_path = 'Filepath & File.xlsx'
df_calendar = pd.read_excel(file_path)

# df_calendar.head()
date = df_calendar['Date']
general_bucket = df_calendar['General Bucket']
start_time = df_calendar['Start Date Time']
end_time = df_calendar['End Date Time']
task = df_calendar['Task']

Update Google Calendar

def create_event(start_date_time, end_date_time, event_title):
  description=None
  location=None

  event = {
    'summary': event_title,
    'location': location,
    'description': description,
    'start': {
    'dateTime': start_date_time,
    'timeZone': 'America/New_York',
    },
 'end': {
    'dateTime': end_date_time,
    'timeZone': 'America/New_York',
    },
 }

return service.events().insert(calendarId=calendar_id, body=event,sendNotifica

import json

df_test=df_calendar[['Start Date Time','End Date Time','Task']]

df_test['Start Date Time'] = df_test['Start Date Time'].apply(lambda x: x.strftime

df_test['End Date Time'] = df_test['End Date Time'].apply(lambda x: x.strftime("%Y

start = df_test['Start Date Time']
end = df_test['End Date Time']
title = df_test['Task']

for i in range(len(df_test)) :
  create_event(start[i],end[i],title[i])
print("Completed")

Sharing

Related Articles

  • All Post
  • Articles
  • Blog Post
  • General Business Automation
  • Portfolio
  • Stock Market & Finance