How do I access (read, write) Google Sheets spreadsheets with Python?

前端 未结 8 850
盖世英雄少女心
盖世英雄少女心 2020-11-29 15:41

I am wondering if you can point me to an example of reading/writing to/from a google doc/spreadsheet using python.

I did look at google docs API here https://develo

8条回答
  •  一向
    一向 (楼主)
    2020-11-29 16:03

    This thread seems to be quite old. If anyone's still looking, the steps mentioned here : https://github.com/burnash/gspread work very well.

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    import os
    
    os.chdir(r'your_path')
    
    scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
    
    creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
    gc = gspread.authorize(creds)
    wks = gc.open("Trial_Sheet").sheet1
    wks.update_acell('H3', "I'm here!")
    

    Make sure to drop your credentials json file in your current directory. Rename it as client_secret.json.

    You might run into errors if you don't enable Google Sheet API with your current credentials.

提交回复
热议问题