The requests library is an excellent and gold standard for Python HTTP requests from Python, however this download style, although not outdated, is unlikely to be preserved, in particular, referring to the download style. In fact, the downloadUrl field in Google Drive API v2 is already out of date . The currently accepted way to export Google Sheets in CSV format is to use the (current) Google Drive API .
So why the Drive API? Shouldn't this be something for API Sheets instead? Well, the Tables API is for tables- oriented functionality, that is, formatting data, resizing a column, creating charts, checking a cell, etc. While the Drive API for a file is oriented functionality, then there is import / export.
Below is the complete cmd-line solution . (If you are not using Python, you can use it as pseudo-code and select any language supported by the Google API Client Libraries .) Snippet, suppose the very last sheet is called inventory (old files with this name are ignored) and DRIVE is the final API service point:
FILENAME = 'inventory' SRC_MIMETYPE = 'application/vnd.google-apps.spreadsheet' DST_MIMETYPE = 'text/csv' # query for latest file named FILENAME files = DRIVE.files().list( q='name="%s" and mimeType="%s"' % (FILENAME, SRC_MIMETYPE), orderBy='modifiedTime desc,name').execute().get('files', []) # if found, export Sheets file as CSV if files: fn = '%s.csv' % os.path.splitext(files[0]['name'].replace(' ', '_'))[0] print('Exporting "%s" as "%s"... ' % (files[0]['name'], fn), end='') data = DRIVE.files().export(fileId=files[0]['id'], mimeType=DST_MIMETYPE).execute() # if non-empty file if data: with open(fn, 'wb') as f: f.write(data) print('DONE')
If your sheet is large, you may need to export it to pieces - see this page on how to do this. If you are new to the Google APIs at all, I have a (somewhat outdated, but) convenient intro video for you. (After that, there are 2 videos that may be useful.)
wescpy
source share