Download a spreadsheet from Google Docs using Python -


Do you have a Google Docs spreadsheet in your key and worksheet ID ( gid )? I can not

I've scratched version 1, 2 and 3 of the API. I do not have any luck, I can not understand their composite Atom-like Feeds API, gdata.docs.service.DocsService.DownloadFile Personal method says that I am unauthorized, and I don a whole I do not want to write the Google login authentication system myself

I have some spreadsheets and I want to use them like this:

  Username = 'mygooglelogin @ gmail Com 'password = getpass.getpass () def get_spreadsheet (key, gid = 0): ... (help!) ... get_spreadsheet For Finance ( '5a3c7f7dcee4b4f'): CELL1, CELL2, CELL3 = line ...  

Please save my face.


Update 1: I have tried the following, but download () or export () To work ( DocsService for docs)

  import gdata.docs.service import getpass import os import tempfile import csv def get_csv (FILE_PATH): return csv. Reader (file (FILE_PATH) .readlines ()) get get_spreadsheet (key, gid = 0): gd_client = gdata.docs.service.DocsService () gd_client.email = 'xxxxxxxxx@gmail.com' gd_client.password = getpass.getpass () Gd_client.ssl ​​= false gd_client.source = "My Fancy Spreadsheet Downloader" gd_client.ProgrammaticLogin () FILE_PATH = tempfile.mktemp ( Root = '. Csv') uri = 'try http://docs.google.com/feeds/documents/private/full/% s'% key: login = gd_client.GetDocumentListEntry (URI) # XXXX - The following RequestError "unauthorized "Gd_client.Download (login, FILE_PATH) return get_csv (FILE_PATH) dies in the end: Try: os.remove (FILE_PATH) Excluding OSError: Pass  

If anyone looks for a quick fix on this, here it is not dependent on the GDT client library:

 < Code> #! / Usr / bin / python import again, urlib, urllib2 class spreadsheet (object): def __init __ (self, key): super (spreadsheet, itself) .__ init (__) self.key = key class customer (object): Def __init __ (auto, email, password): super (client, self) .__ init __ () self.email = email self.password = password def _get_auth_token (auto, email, password, source, service): url = " "HOSTED_OR_GOOGLE", "source:", "account type" Req = urllib2.Request (url, urllib.urlencode (parameter)) back refund (r "at = ( *) ", Urllib2.urlopen (Req) .read ()) [0] def get_auth_token (self): source = type (self) .__ name return back self._get_auth_token (self.email, self.password, source, service = "Wise") DEF Download (auto, spreadsheet, gid = 0, format = "CSV"): url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat =% S & Gid =% i "header = {" authorization ":" GoogleLogin auth = "+ self.get_auth_token ()," GData-Version ":" 3.0 "} req = urllib2.Request (url_format% (spreadsheet.key) , Format, Gid), Header = Header) return urlib2.urlopen (req) if __name__ == "__main__": import getpass import csv email = "# (your email here) password = getpass.getpass () spreadsheet_id ="#" (Spreadsheet ID here) # Create customer and spreadsheet object GS = Client (email, password) SS = spreadsheet (ID) # Request an item like a file containing contents of the spreadsheet csv_file = gs.download (ss) # Print pars and rows in the form of CSV for the row in Csv.reader (csv_file): Print "," .join (line)  

Comments