1

Background

Hi All...still pretty new to python. I'm on a Mac (Sierra) running Jupyter Notebook in Firefox (87.0). I'm trying to use my python script to build a dataframe, create a new Google Sheet (a new workbook, not a new sheet in an existing workbook) on my personal Google Drive in a specific folder, and then write my dataframe to that new Google Sheet.

What I've Tried

I've been able to get my service account to open an existing Google Sheet and then later read/write using code like this:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import set_with_dataframe

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_path, scope)
client = gspread.authorize(creds)
workbook = client.open_by_key(my_existing_google_sheet_key)

There's tons of documentation on how to read/write from existing Google Sheets, but as I said I'd like to create a new Google Sheet to write to. This has been surprisingly hard to find. The one reference I did find was this related post but can't get the OP's code to work (what is 'discovery'?). I feel like I'm missing something obvious.

Questions

  1. How do I use python to have my service account create a new Google Sheet on my personal Google Drive in a specific folder so that I can write to it?

1 Answer 1

4

I believe your goal as follows.

  • You want to create new Google Spreadsheet to the specific folder of your own Google Drive using the service account.
  • You want to achieve this using gspread with python.

Modification points:

  • When I saw the document of gspread, it seems that the create method of Class Client can be used. And, in this method, the folder ID can be used for putting to the specific folder.

When these are reflected to your script, it becomes as follows.

Modified script:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import set_with_dataframe

json_path = '###' # Please set the file for using service account.

scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_path, scope)
client = gspread.authorize(creds)

spreadsheetTitle = 'new spreadsheet title'
folderId = '###' # Please set the folder ID of the folder in your Google Drive.

workbook = client.create(spreadsheetTitle, folder_id=folderId)

Note:

  • In this case, it is required to share the specific folder of your Google Drive with the email of the service account. By this, the Spreadsheet can be created to the folder with the service account. Please be careful this.

Reference:

Sign up to request clarification or add additional context in comments.

2 Comments

@Fist Pump Cat Now, I noticed that the create method of Class Client has the argument for putting to the specific folder when the new Spreadsheet is created. I apologize for this. When this is used, the script becomes simpler. So I updated my answer. Could you please confirm it?
this does work for creating the sheet in a folder in the user account, but technically the sheet is still owned by the service account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.