mathgeek
mathgeek

Reputation: 125

Uploading data values to google sheets without service account? PYTHON

I have been using this link to read spreadsheets in Python, which works perfectly well: https://predictivehacks.com/the-easiest-way-to-connect-python-with-google-sheets/

However, I noticed there isn't any implementation for it to write and push datavalues into a spreadsheet on google. I did some research on google and noticed that the alternative ways seem to be to use a service account and share the spreadsheet to the service account email so that it can access it.

Note: I have the ID of the specific spreadsheet that I want to access

Questions:

Thanks

Upvotes: 0

Views: 1715

Answers (1)

Nikko J.
Nikko J.

Reputation: 5543

Use gspread library. You can use OAuth Client ID to authenticate your application in gspread.

  1. Enable API Access for a Project if you haven’t done it yet.
  2. Go to “APIs & Services > OAuth Consent Screen.” Click the button for “Configure Consent Screen” and follow the directions to give your app a name; you don’t need to fill out anything else on that screen. Click Save.
  3. Go to “APIs & Services > Credentials”
  4. Click “+ Create credentials” at the top, then select “OAuth client ID”.
  5. Select “Desktop app”, name the credentials and click “Create”. Click “Ok” in the “OAuth client created” popup.
  6. Download the credentials by clicking the Download JSON button in “OAuth 2.0 Client IDs” section.
  7. Move the downloaded file to ~/.config/gspread/credentials.json. Windows users should put this file to %APPDATA%\gspread\credentials.json.

For updating cells or range you can use gspread's update method:

Example:

worksheet.update('A1:B2', [[1, 2], [3, 4]])

References:

Upvotes: 3

Related Questions