1

I am trying to download a google spreadsheet in the form of CSV. But the script will always redirect me to login page even I provided the user name and password. Any suggestion to fix this?

import requests
from requests.auth import HTTPBasicAuth

response = requests.get('https://docs.google.com/spreadsheet/ccc?key=1Kwim1GF0SXx24kN8LycaYF6LUsJAIAm6FkPStDqDoNE&output=csv',auth=HTTPBasicAuth('xxxxxx', 'xxxxxxx'))
data=response.content
csv_file = open(r"C:\google_sheet\bills.csv", "w")
csv_file.write("%s" % data)
csv_file.close()
Yang L
  • 331
  • 1
  • 9

1 Answers1

0

To do this without google python client, see this post.

But you may want to use the google cloud python client. Using the client, you would want to access the google drive api; the sheets api is more about manipulating sheets so don't use that.

Follow the quickstart to (1) get connectivity in the form of a service object and (2) list the ids for your spreadsheets.

To authenticate your service object, you'll need to generate a credentials file. Then you'll use the service object to make your api calls (listing and downloading).

With service object and file id in hand, you download a spreadsheet as csv like so:

file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
                                             mimeType='text/csv')
with open('/path/to/outfile.csv', 'w') as fh:
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print "Download %d%%." % int(status.progress() * 100)
dstandish
  • 1,685
  • 13
  • 25