Skip to content

Instantly share code, notes, and snippets.

@kitschpatrol
Created July 30, 2012 14:58
Show Gist options
  • Save kitschpatrol/3207582 to your computer and use it in GitHub Desktop.
Save kitschpatrol/3207582 to your computer and use it in GitHub Desktop.
Google Docs Spreadsheet to CSV
#!/usr/bin/python
# Script to automate downloading CSV files from GoogleDocs
#
# Expects a config file in the same folder called google_docs_to_file.cfg
# with the following format:
#
# [google_credentials]
# email = someone@somewhere.com
# password = *********
import re, urllib, urllib2, ConfigParser
config = ConfigParser.RawConfigParser()
config.read('google_docs_to_file.cfg')
class Spreadsheet(object):
def __init__(self, key, gid=0):
super(Spreadsheet, self).__init__()
self.key = key
self.gid = gid
class Client(object):
def __init__(self, email, password):
super(Client, self).__init__()
self.email = email
self.password = password
def _get_auth_token(self, email, password, source, service):
url = "https://www.google.com/accounts/ClientLogin"
params = {
"Email": email, "Passwd": password,
"service": service,
"accountType": "HOSTED_OR_GOOGLE",
"source": source
}
req = urllib2.Request(url, urllib.urlencode(params))
return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]
def get_auth_token(self):
source = type(self).__name__
return self._get_auth_token(self.email, self.password, source, service="wise")
def download(self, spreadsheet, gid=0, format="csv"):
url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
headers = {
"Authorization": "GoogleLogin auth=" + self.get_auth_token(),
"GData-Version": "3.0"
}
req = urllib2.Request(url_format % (spreadsheet.key, format, spreadsheet.gid), headers=headers)
print "\nDownloading from " + req.get_full_url()
return urllib2.urlopen(req)
if __name__ == "__main__":
import getpass
import csv
# Conveniently grab this stuff
def googleDocsToFile(key, gid, destination):
# Create client and spreadsheet objects
gs = Client(config.get("google_credentials", "email"), config.get("google_credentials", "password"))
ss = Spreadsheet(key, gid)
# Request a file-like object containing the spreadsheet's contents
csv_file = gs.download(ss)
# Write to file, and fix line endings too
print "Writing to " + destination
f = open(destination, 'wr+')
for line in csv_file.readlines():
f.write(line.rstrip('\r\n') + "\r\n")
f.close()
# Run the batch
print "Starting CSV download batch..."
# Download the spreadsheet
googleDocsToFile("KEY_VALUE_FROM_GDOC_URL_HERE", SHEET_GID_HERE, "~/Desktop/Destination_CSV_Here.csv")
print "... done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment