Python 如何使用csv中的Pydrive从Google sheet下载特定工作表
如果我需要使用Python和pydrive下载特定的google工作表,我可以使用如下简单命令:Python 如何使用csv中的Pydrive从Google sheet下载特定工作表,python,google-sheets,google-drive-api,google-sheets-api,pydrive,Python,Google Sheets,Google Drive Api,Google Sheets Api,Pydrive,如果我需要使用Python和pydrive下载特定的google工作表,我可以使用如下简单命令: drive.CreateFile({'id':'[sheetID]}).GetContentFile('file.csv', mimetype='text/csv') 这是可行的,但在本例中,我只下载谷歌文件的第一张表单 如果此文件中有多张工作表,我需要下载一张特定的工作表,可能是通过我在URL中看到的ID“…edit#gid=11111 5555”,我如何更改命令 我想下载一张纸,不管它的位置或
drive.CreateFile({'id':'[sheetID]}).GetContentFile('file.csv', mimetype='text/csv')
这是可行的,但在本例中,我只下载谷歌文件的第一张表单
如果此文件中有多张工作表,我需要下载一张特定的工作表,可能是通过我在URL中看到的ID“…edit#gid=11111 5555”,我如何更改命令
我想下载一张纸,不管它的位置或名称
你能帮我吗
提前谢谢我相信你的目标如下
- 您想在Google电子表格中下载特定的表格作为CSV数据
- 您希望使用pydrive和python来实现您的目标
- 运行上述脚本时,
的电子表格中的spreadsheetId
的特定工作表将作为CSV数据下载并创建为文件sheetId
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import requests
# Script for authorization of pydrive.
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
# Download the specific sheet in Google Spreadsheet as a CSV data.
spreadsheetId = '###' # Please set the Spreadsheet ID.
sheetId = '###' # Please set the sheet ID. (GID)
url = 'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/gviz/tq?tqx=out:csv&gid=' + sheetId
headers = {'Authorization': 'Bearer ' + gauth.credentials.access_token}
res = requests.get(url, headers=headers)
with open('file.csv', 'wb') as f:
f.write(res.content)