Skip to content

Instantly share code, notes, and snippets.

@sdolenc
Last active June 23, 2023 14:25
Show Gist options
  • Save sdolenc/4a3dc2a8ef79cee1acdab625eb808c85 to your computer and use it in GitHub Desktop.
Save sdolenc/4a3dc2a8ef79cee1acdab625eb808c85 to your computer and use it in GitHub Desktop.
Import Recurring Events to Google Calendar

One method of quickly creating a lot of events in google calendar is to import a CSV file. However, there is no support for directly adding "recurring" events (birthdays, anniversaries, etc). Here's the workarond.

Summarized steps

  1. create csv file with events (no recurring)
  2. import csv into a new, temporary google calendar
  3. export temporary google calendar to an ics file
  4. edit ics file to change events into recurring
  5. import ics file into a new, permanent google calendar.
  6. delete temporary google calendar

Detailed steps

1. create csv file with events (no recurring)

The CSV format can be found at https://support.google.com/calendar/answer/37118?hl=en

example csv

2. import csv into a new, temporary google calendar

3. export temporary google calendar to an ics file

4. edit ics file to change events into recurring

4a. manually by adding RRULE:FREQ=YEARLY between each set of VEVENT begin/close statements. For example:

BEGIN:VEVENT
DTSTART;VALUE=DATE:20180520
DTEND;VALUE=DATE:20180521
RRULE:FREQ=YEARLY
...
END:VEVENT

or

4b. automatically by running this python

import icalendar
import os

def make_recurring():
    directory = os.path.dirname(__file__)
    with open(os.path.join(directory, 'noRecur.ics'), 'r') as fr:
        data = fr.read()

    cal = icalendar.Calendar.from_ical(data)
    for event in cal.subcomponents:
        if 'RRULE' not in event.keys():
            event.add('rrule', {'freq': ['YEARLY']})

    with open(os.path.join(directory, 'withRecur.ics'), 'wb') as fw:
        fw.write(cal.to_ical())

5. import ics file into a new, permanent google calendar.

6. delete temporary google calendar

@Hsad1644
Copy link

Hsad1644 commented Aug 2, 2021

The code below is ready to use requiring you to add the path of the noRecur.ics file in __file__ (the one exported from the temp calendar)

Code

import icalendar
import os
__file__ = "" # paste full path of the noRecur.ics file here within the double quotes
# the directories can be separated using //
# EXAMPLE - C://Users//apa//OneDrive//Documents//billybob@gmail.com.ical//noRecur.ics


def make_recurring():
    directory = os.path.dirname(__file__)
    with open(os.path.join(directory, 'noRecur.ics'), 'r') as fr:
        data = fr.read()

    cal = icalendar.Calendar.from_ical(data)
    for event in cal.subcomponents:
        if 'RRULE' not in event.keys():
            event.add('rrule', {'freq': ['WEEKLY']}) # /!\ Change frequency of recurrence as per requirement here in all caps
# WEEKLY, YEARLY and MONTHLY are valid not so sure about DAILY

    with open(os.path.join(directory, 'withRecur.ics'), 'wb') as fw:
        fw.write(cal.to_ical())


make_recurring()

Running the code

Once the code is saved on your system, say as test.py, open sh or cmd and type -

python test.py

@sdolenc
Copy link
Author

sdolenc commented Aug 2, 2021

Thanks @Hsad1644 !

I'll eventually try out your suggested version and update the gist accordingly 👍

@Hsad1644
Copy link

Hsad1644 commented Aug 3, 2021

Thanks @Hsad1644 !

I'll eventually try out your suggested version and update the gist accordingly 👍

Its pretty much the same thing
I just added some comments and that one line to call make_recurring() 😃

Aimed at python amateurs. That's all.

Much thanks for this. I'm trying to incorporate it into a web app which will be useful for preparing a timetable from an .xlsx file for students in my university.

@megalois
Copy link

Great work, thanks!

@dzigner
Copy link

dzigner commented Feb 3, 2022

@Hsad1644
Just ran the python code - thank you. it works.

@sdolenc
Copy link
Author

sdolenc commented Feb 3, 2022

@dzigner i think running
pip install icalendar
will fix this

https://pypi.org/project/icalendar/

@dzigner
Copy link

dzigner commented Feb 3, 2022

@sdolenc
I have updated my comment - thanks

@gprehder
Copy link

Thank you for the code, it help me a lot!! One question, I've tried different ways but I didn't get it to work with recurring UNTIL. I always get an error stating that I should use datetime, but it does not work.

Thank you

@abvgu
Copy link

abvgu commented Jan 9, 2023

Greetings! I needed to bulk upload a lot of annual events to the Google calendar. Your code worked great. Thanks a lot!

@okimselim
Copy link

Why waste time with python? Just open the .ics in a text editor and find/replace all

END:VEVENT

with

RRULE:FREQ=YEARLY
END:VEVENT

Done in 2 seconds.

@Hsad1644
Copy link

Hsad1644 commented Feb 13, 2023

Yeah I lost that script to my cache memory xP (so thanks for your comment since you led me back to it). This is what i do now. alt + D on vscode helps a bunch because I use this for making my semester's weekly schedule.

The script was more like a solution to add to an app that I have planned to make for our uni.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment