Create a CSV of Yesterday's Google Calendar Events
Tags: python, scripting, csv, googleapis
Published: 2017-10-11 21:14:54

---
title: "Create a CSV of Yesterday's Google Calendar Events"
published: true
tags: python,scripting,csv,Google APIs
canonical_url: http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/
---

 ![Create a CSV of Yesterday's Google Calendar Events](http://scriptingwithpython.com/content/images/2017/09/gregorian-calendar.jpg)

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](http://scriptingwithpython.com/my-lazy-scripter-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.

## What is the Task?

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.

| title | start\_date | end\_date | duration |
| --- | --- | --- | --- |
| Lunch | 2017-09-21 09:00:00-06:00 | 2017-09-21 09:55:00-06:00 | 55 |

## Solution to pulling Google Calendar Events to CSV

### Pre-requisites

- Python 2.7, preferably Python 3.4 or higher [see my take on Python versions](https://dev.to/scriptingwithpy/my-take-on-python-version-2-vs-3-c70-temp-slug-9579522)
- a good text editor or Python IDE <sup><a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fn1" id="fnref1">[1]</a></sup>
- A Google account
- Operating systems:
  - Linux
  - Mac OS X
  - Windows
- Python 2.7, or 3.4 or higher

### Summary

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 <sup><a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fn2" id="fnref2">[2]</a></sup>. 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."  
> -- Me

So that's what I did.

I modified the code I found on that page to export a CSV<sup><a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fn3" id="fnref3">[3]</a></sup> 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.

## Ready, Go!

### Step 1 - Setup Environment

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](https://dev.to/scriptingwithpy/setting-up-a-python-virtual-environment-29d-temp-slug-5994947) 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](https://dev.to/scriptingwithpy/setting-up-a-python-virtual-environment-29d-temp-slug-5994947) if you aren't sure how to do this. Go ahead and do this now.

I'll wait...

![Create a CSV of Yesterday's Google Calendar Events](https://images.unsplash.com/photo-1503457917564-4635cba0adf5?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=16a1c8130c4821070326b4385a2a379f)  
<small>Photo by <a href="https://unsplash.com/@jaime210?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Jaime Top</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></small>

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.

### Step 2 - Turn on the Google Calendar API

Follow the "Turn on the Google Calendar API" steps at [https://developers.google.com/google-apps/calendar/quickstart/python](https://developers.google.com/google-apps/calendar/quickstart/python).

### Step 4 - Create the Python file `pull_from_gcal.py`

Create the Python file to run this script. You may want to create a new project if you are using PyCharm.

### Step 5 - Save the code

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.

<script src="https://gist.github.com/kajigga/90b27d826b2025a07f49f53e2a354028.js"></script>
### Step 6 - Run the script

Run the by right-clicking on the file in PyCharm and select `Run 'pull_from_gcal.py'`.

![Create a CSV of Yesterday's Google Calendar Events](http://scriptingwithpython.com/content/images/2017/09/Screen-Shot-2017-09-24-at-6.33.31-PM.png)

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.

![Create a CSV of Yesterday's Google Calendar Events](http://scriptingwithpython.com/content/images/2017/09/Screen-Shot-2017-09-24-at-9.30.13-AM.png)

And the CSV with yesterday's events will be created as well.

![Create a CSV of Yesterday's Google Calendar Events](http://scriptingwithpython.com/content/images/2017/09/Screen-Shot-2017-09-24-at-6.37.31-PM.png)

## Wrap-up, Conclusion, Follow-up

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.

* * *
<section>
<ol>
<li id="fn1">
<p>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. <a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fnref1">↩︎</a></p>
</li>
<li id="fn2">
<p>https://developers.google.com/google-apps/calendar/quickstart/python <a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fnref2">↩︎</a></p>
</li>
<li id="fn3">
<p>find more articles on manipulating CSV files at <a href="http://scriptingwithpython.com/pull-a-list-of-yesterdays-google-calendar-events/#fnref3">↩︎</a></p>
</li>
</ol>
</section>