Skip to content

Instantly share code, notes, and snippets.

@Thuruv
Forked from cspickert/GoogleSpreadsheets.py
Created August 23, 2016 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thuruv/dc0e2f781b8e095b9981f265647b8304 to your computer and use it in GitHub Desktop.
Save Thuruv/dc0e2f781b8e095b9981f265647b8304 to your computer and use it in GitHub Desktop.
Export a Google Spreadsheet using python.
#!/usr/bin/python
import re, urllib, urllib2
class Spreadsheet(object):
def __init__(self, key):
super(Spreadsheet, self).__init__()
self.key = key
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, gid), headers=headers)
return urllib2.urlopen(req)
if __name__ == "__main__":
import getpass
import csv
email = "" # (your email here)
password = getpass.getpass()
spreadsheet_id = "" # (spreadsheet id here)
# Create client and spreadsheet objects
gs = Client(email, password)
ss = Spreadsheet(spreadsheet_id)
# Request a file-like object containing the spreadsheet's contents
csv_file = gs.download(ss)
# Parse as CSV and print the rows
for row in csv.reader(csv_file):
print ", ".join(row)
@jlw2002us
Copy link

jlw2002us commented Aug 15, 2018

Hey I copied my google email and google sheet number into this, but I get the below errors when I run it:

  Traceback (most recent call last):
  File "Googlespreadsheets.py", line 53, in <module>
  csv_file = gs.download(ss)
  File "Googlespreadsheets.py", line 34, in download
 "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
  File "Googlespreadsheets.py", line 29, in get_auth_token
  return self._get_auth_token(self.email, self.password, source, 
  service="wise")
 File "Googlespreadsheets.py", line 25, in _get_auth_token
 return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]
 File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
 return opener.open(url, data, timeout)
 File "/usr/lib/python2.7/urllib2.py", line 435, in open
 response = meth(req, response)
 File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
 'http', request, response, code, msg, hdrs)
 File "/usr/lib/python2.7/urllib2.py", line 473, in error
 return self._call_chain(*args)
 File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
  result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
  raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 404: Not Found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment