DEV Community

Kajigga for Scripting With Python

Posted on • Originally published at scriptingwithpython.com on

Create a CSV of Yesterday's Google Calendar Events

Create a CSV of Yesterday's Google Calendar Events

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.

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
  • a good text editor or Python IDE [1]
  • 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 [2]. 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[3] 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 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.

I'll wait...

Create a CSV of Yesterday's Google Calendar Events

Photo by Jaime Top / Unsplash

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.

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.

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

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

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

Create a CSV of Yesterday's Google Calendar Events

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.


  1. 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. ↩︎

  2. https://developers.google.com/google-apps/calendar/quickstart/python ↩︎

  3. find more articles on manipulating CSV files at ↩︎

Top comments (5)

Collapse
 
sanjay555 profile image
Sanjayshr • Edited

This is neat !! How do I take this script/app to cloud(Heroku) and pull data from the calendar and save it in CSV file whenever I add a new event in google calendar ?

Collapse
 
kajigga profile image
Kajigga

That, my friend, would be an interesting project. I may consider writing an article around it someday. It would too long to describe the whole thing in this comment but these steps might get you started.

  • Start a new project with a web framework such as Flask or Django
  • Add this code to the project
  • create views to see the calendar events
  • create a command line utility to poll for new events
  • schedule this utility to run every day (using crontab or something similar)
Collapse
 
sanjay555 profile image
Sanjayshr • Edited

Awesome this answer was expected !! However, That is my first step to build personal assistant chatbot, As a beginner python/flask developer, I love to hear from you more on this.

Thread Thread
 
kajigga profile image
Kajigga

Flask would be a great platform for a basic web app illustrating how to do this. I may put a small project together to illustrate how to do this Calendar polling in a webapp and post an article on it.

Thread Thread
 
sanjay555 profile image
Sanjayshr

Awesome eagerly waiting for that.