【发布时间】:2022-12-11 22:04:37
【问题描述】:
我试图在不在本地下载文件的情况下读取谷歌驱动器上的 excel 文件
credentialFileOfServiceAccount = 'x.json'
scope = ['https://www.googleapis.com/auth/drive'] #edited by ashraf
creds = ServiceAccountCredentials.from_json_keyfile_name(credentialFileOfServiceAccount, scope)
print(creds)
spreadsheetId=file.get("id")
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
res = requests.get(url, headers={"Authorization": "Bearer " + gauth.attr[creds].access_token})
# 2. The downloaded XLSX data is read with `pd.read_excel`.
sheet = "Sheet1"
values = pd.read_excel(BytesIO(res.content), usecols=None, sheet_name=sheet)
print(values)
那是我收到的错误
InvalidConfigError:无效的客户端机密文件('打开文件时出错','client_secrets.json','没有这样的文件或目录',2)
【问题讨论】:
-
仅供参考:您正在将文件下载到内存中,只是没有将其保存到计算机上的文件中。你的标题有点偏。对于该代码如何导致该错误,我有点困惑。错误状态为 client_secrets.json,但您的代码显示为 x.json。也就是说,您的代码找不到文件 client_secrets.json 确保 credentialFileOfServiceAccount 实际上指向文件所在的目录。
-
你能验证这是一个 xlsx 文件而不是一个已经转换为谷歌表格的文件吗?
标签: python pandas google-drive-api