Python is great at processing structured data. There are all sorts of libraries that you can use to parse JSON, CSV, XML, etc. There are libraries to parse calendar (.ics) files as well.
A few days ago, I needed to pull a CSV report of all of my calendar events from the day before. This task might need to be done more than once. My scripting radar immediately picked up a ping and I saw a perfect target for a Python script.
Sure, I could copy and paste the list from the Agenda view in Google Calendar itself. That could be ok if I only needed to do this once. However, I might do this again and a script gives me way more flexibility and frees me up to do other things.
Task: Pull a complete list of all calendar events from Google Calendar and save it in a CSV file. The output CVS file must have the following columns.
|Lunch||2017-09-21 09:00:00-06:00||2017-09-21 09:55:00-06:00||55|
- Python 2.7, preferably Python 3.4 or higher see my take on Python versions
- a good text editor or Python IDE 
- A Google account
- Operating systems:
- Mac OS X
- Python 2.7, or 3.4 or higher
I searched around for a while for a good Python Google Calendar module. I did find several that showed some potential but most seemed a bit dated. I ended up on Google's own developer page . On this page I found a Python example doing much of the process I was looking for. I thought to myself:
"Self, there is no reason to recreate something good. Let's take what is good here and modify it to fit our needs."
So that's what I did.
I modified the code I found on that page to export a CSV file with the values I need. You may also noticed that I adjusted it to nicely pull in more than the 10 events the sample script did. You'll see this later.
I am mostly satisfied with the end result. It does what it needs to much faster and arguably better than I could manually--and that's the point.
Much of the instruction on this page comes straight from the Google article.
The first thing to do is install the Google Calendar Python client library.
Before doing that, however, you need to setup a virtual environment. I always recommend using a virtualenvironment when installing Python libraries, especially when you're not sure what it does :) .
If you haven't setup a virtual environment to work on, please do so now. Visit virtualenvironment if you aren't sure how to do this. Go ahead and do this now.
Make sure you are in your virtualenvironment before doing the next steps.
In a terminal window, enter the following command:
$ pip install --upgrade google-api-python-client dateutil
You should not be prompted for your password to install the package. If you are, then you either didn't setup the virtual environment or you are not working in it. Go back to that page and make sure it is setup correctly.
Follow the "Turn on the Google Calendar API" steps at https://developers.google.com/google-apps/calendar/quickstart/python.
Create the Python file to run this script. You may want to create a new project if you are using PyCharm.
Here you can copy/paste the finished code if you want to jump straight to running it. However, you may benefit from a line-by-line (mostly) walk-through to make sure you know what it is doing.
Run the by right-clicking on the file in PyCharm and select
If all goes well, the script will open a browser window where you will be prompted to approve the application. Go ahead and do this. The script will gather the necessary credentials and save them to a local file for future use.
You browser should show a screen like the following if it worked.
And the CSV with yesterday's events will be created as well.
Well, there you go. You now have a script that will save you time if you ever need to do this. Thank you for taking the time to read through and possibly testing this script.
Here are some potential enhancements to the script you may want to consider making.
- Pull events from all of your calendars
- Add field with list of attendees
- Add field indicating if the event is a recurring event
- Add field indicating if you created the event or if you were invited
I am working on an ebook with lots of scripting recipes. I plan to include this one with some of these changes. Stay tuned to learn more. I encourage you to subscribe using the form below.
I Love Pycharm for learning Python. You may see me using vim a lot because I have so much muscle memory with it. PyCharm is so much better for testing and editing code with greate syntax highlighting and code hints. ↩︎
find more articles on manipulating CSV files at ↩︎