Read Google docs spreadsheet using Python
One of the many useful and productive feature for me personally is to programmatically read a Google docs spreadsheet in Python. This is very useful to automate a task to insert some data in MySQL/NOSQL/ElasticSearch, which would have otherwise required to build a custom user interface for data input.
Reading is actually quite simple. For example, you have a spreadsheet like this:
First, you need to grab the file id. In the image below, file id is
19Y_Oi5_riecwonPbtxN4sfDntZO62s_vJbXoogFFp9o
Make sure to give the read permission to anyone having the link, otherwise our python program won’t be able to read the file.
First, we make a simple GET
request on the export url of the spreadhseet using the requests module
Once we have the response, it is easy to read it using the csv module
For example, to read the data as a dictionary, we can do something like this:
This will print the following output on console:
Incase, we are using unicode characters with our file. We can make use of unicodecsv module. It is a drop in replacement of the csv
module.
The complete program is given in this gist and as well as below. You just need to replace the file_id
parameter to start using it. Feel free to fork it :)