1

I can already edit an existing sheet by using the open_by_key() function but I would like to be able to create a brand-new sheet.

When I try the following code:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
sh = gc.open("my_1st_public_sheet")
wks = sh.sheet1

# Update a single cell.                                                                                                                                                                                                                                                                                                               
wks.update_value('A1', "some value")

sh.share('', role='reader', type='anyone')

I get this exception/error:

raise SpreadsheetNotFound('Could not find a spreadsheet with title %s.' % title) pygsheets.exceptions.SpreadsheetNotFound: Could not find a spreadsheet with title my_1st_public_sheet.

What am I missing?

Jerome Provensal
  • 931
  • 11
  • 22

1 Answers1

6
  • You want to create new sheet using pygsheets with python.

If my understanding is correct, how about this answer? From your question, I could understand as following 2 patterns.

Pattern 1:

  • You want to add new sheet in an existing Spreadsheet.

In this case, the sample script is as follows.

Sample script:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

spreadsheet_key = '###'  # Please set the Spreadsheet ID.
gc = pygsheets.authorize(service_file=creds_file)
sh = gc.open("my_1st_public_sheet")
sh.add_worksheet("sampleTitle")  # Please set the new sheet name.

Pattern 2:

  • You want to create new Spreadsheet.

In this case, the sample script is as follows.

Sample script:

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
res = gc.sheet.create("sampleTitle")  # Please set the new Spreadsheet name.
print(res)
  • In this case, the new Spreadsheet is created to the Drive of sercice account. So if you want to see the created Spreadsheet in your Google Drive, please share the Spreadsheet with your Google account. Please be careful this.

Note:

  • These sample script supposes that you have already been able to get and put values for Spreadsheet using Sheets API.

References:

If I misunderstood your question, I apologize.

Added:

In the following sample script, new Spreadsheet is created and the created Spreadsheet is shared with your account.

Sample script:

Before you run the script, please set your email address of Google account to ### your email address ###.

import pygsheets

creds_file = "/Users/jerome/audible2googlesheet.json"

gc = pygsheets.authorize(service_file=creds_file)
res = gc.sheet.create("sampleTitle")  # Please set the new Spreadsheet name.
createdSpreadsheet = gc.open_by_key(res['spreadsheetId'])
createdSpreadsheet.share('### your email address ###', role='writer', type='user')
  • About the detail settings of share, please check the document.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I want to create a brand-new, whole sheet/document from scratch and not add worksheet to an existing sheet. You said above that I need to "share the Spreadsheet with your Google account". How do I do that? Can it be done programmatically? – Jerome Provensal Nov 30 '19 at 00:00
  • @Jerome Provensal Thank you for replying. I deeply apologize for my poor English skill. From your replying, the pattern 2 can be used. About the method for sharing the created Spreadsheet, I added one more sample script. Could you please confirm it? If that was not the result you want, I apologize. – Tanaike Nov 30 '19 at 01:34