在Team Drive和Colab中创建新的电子表格时,Gspread File未找到错误



我在谷歌Colab工作,试图处理一些数据,并将其作为谷歌电子表格保存到我管理的团队驱动器中。我使用的是gspread 3.6.0。

我试图保存到的文件夹已经存在(我自己创建的(。这是我的测试代码:

#Authentication
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())    
test_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
folder_id = '1piOfOFFw60hh5SbC9gIPAveCk90thI9_'
sh = gc.create('test_df', folder_id=folder_id)

gspread抛出的错误如下:

---------------------------------------------------------------------------
APIError                                  Traceback (most recent call last)
<ipython-input-38-a6c430392153> in <module>()
2 folder_id = '1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR'
3 
----> 4 sh = gc.create('df_prueba', folder_id=folder_id)
1 frames
/usr/local/lib/python3.6/dist-packages/gspread/client.py in create(self, title, folder_id)
194             payload['parents'] = [folder_id]
195 
--> 196         r = self.request('post', DRIVE_FILES_API_V3_URL, json=payload)
197         spreadsheet_id = r.json()['id']
198         return self.open_by_key(spreadsheet_id)
/usr/local/lib/python3.6/dist-packages/gspread/client.py in request(self, method, endpoint, params, data, json, files, headers)
71             return response
72         else:
---> 73             raise APIError(response)
74 
75     def list_spreadsheet_files(self, title=None):
APIError: {'errors': [{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR.', 'locationType': 'parameter', 'location': 'fileId'}], 'code': 404, 'message': 'File not found: 1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR.'}

我从团队驱动器中读取文件没有问题。

如果我将folder_id更改为"我的驱动器"中的一个文件夹,代码就会工作。我已经检查了gspread文档,但在API中没有发现任何与团队驱动特别相关的参数。

有线索吗?

它并不优雅,但您可以使用Drive API。试试下面的代码。

from googleapiclient.discovery import build
folder_id = "1piOfOFFw60hh5SbC9gIPAveCk90thI9_"
title = "test_df"
body = {
"name": title,
"mimeType": "application/vnd.google-apps.spreadsheet",
"parents": [folder_id]
}
service = build("drive", "v3", credentials=CREDENTIALS)
service.files().create(body=body, fields="id", supportsAllDrives=True).execute()

我遇到了这个问题。我们能够通过更新gspread,pip install -U gspread来解决这个问题。(我们从3.6.0升级到4.0.1(

最新更新