[英]google service account not finding .xlsx files
I am trying to use a google drive folder to store data then read and manipulate it with python using gspread.我正在尝试使用谷歌驱动器文件夹来存储数据,然后使用 gspread 使用 python 读取和操作它。 The steps I followed in order to try and do this are:
为了尝试执行此操作,我遵循的步骤是:
import gspread
# open credentials file and connect to google drive
gc = gspread.service_account(filename='credentials.json')
# list all spread sheets with access from credentials (returning empty list)
gc.list_spreadsheet_files()
My result is always the same, an empty list suggesting that my service account has access to no spread sheets, but when I look at my.xlsx files they all say that the service account email has been added as an editor.我的结果总是一样的,一个空列表表明我的服务帐户无法访问任何电子表格,但是当我查看 my.xlsx 文件时,他们都说服务帐户 email 已被添加为编辑器。
What could be the purpose of this and what would a good solution be?这样做的目的是什么,好的解决方案是什么?
I believe your goal is as follows.我相信你的目标如下。
gc.list_spreadsheet_files()
returns only Google Spreadsheet.gc.list_spreadsheet_files()
只返回谷歌电子表格。 So, in your expected goal, from your reply, I understood that you wanted to get the file list of both Google Spreadsheet files and Microsoft Excel files. In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?
import gspread
from googleapiclient.discovery import build
gc = gspread.service_account(filename='credentials.json')
service = build("drive", "v3", credentials=gc.auth)
fileList = {"spreadsheet": [], "excel": []}
pageToken = ""
while pageToken is not None:
res = service.files().list(q="(mimeType='application/vnd.google-apps.spreadsheet' or mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') and trashed=false", fields="nextPageToken, files(id,name,mimeType)", pageSize=1000, pageToken=pageToken, corpora="allDrives", includeItemsFromAllDrives=True, supportsAllDrives=True).execute()
for e in res.get("files", []):
if e["mimeType"] == "application/vnd.google-apps.spreadsheet":
del e["mimeType"]
fileList["spreadsheet"].append(e)
elif e["mimeType"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
del e["mimeType"]
fileList["excel"].append(e)
pageToken = res.get("nextPageToken")
print(fileList)
When this script is run, the following result is obtained.运行此脚本时,将获得以下结果。
{
'spreadsheet': [{'id': '###', 'name': '###'}, {'id': '###', 'name': '###'},,,],
'excel': [{'id': '###', 'name': '###'}, {'id': '###', 'name': '###'},,,]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.