【发布时间】:2019-07-15 01:40:03
【问题描述】:
为了减少对 Sheets API 的 API 调用次数并避免可怕的“错误 429”消息,我希望使用 Sheets API 的“batchGet”函数。我已将所有相关信息放入一个谷歌电子表格spreadsheet_id,其中包含多个工作表ranges。下一步是将此 batchGet 请求转换为 Pandas Dataframe。
这是我的代码...如果有人可以就下一步将其导入 pandas df 提供指导,那就太好了。
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
SCOPES = [ 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', SCOPES)
service = discovery.build('sheets', 'v4', credentials=credentials)
# The ID of the spreadsheet to retrieve data from.
spreadsheet_id = 'my_spreadheet_id' # TODO: Update placeholder value.
# The A1 notation of the values to retrieve.
ranges = ['2016_IGA!A2:BD', '2017_IGA!A2:BD', '2018_IGA!A2:BD', '2019_IGA!A2:BD', '2020_IGA!A2:BD',
'2016_Coles!A2:BD', '2017_Coles!A2:BD', '2018_Coles!A2:BD', '2019_Coles!A2:BD', '2020_Coles!A2:BD', # TODO: Update placeholder value.
'2016_WW!A2:BD', '2017_WW!A2:BD', '2018_WW!A2:BD', '2019_WW!A2:BD', '2020_WW!A2:BD',
'2018_Aldi!A2:BD', '2019_Aldi!A2:BD', '2020_Aldi!A2:BD']
value_render_option = 'FORMATTED_VALUE'
request = service.spreadsheets().values().batchGet(spreadsheetId=spreadsheet_id, ranges=ranges, valueRenderOption=value_render_option)
response = request.execute()
【问题讨论】:
-
为了正确理解您的问题,您能否提供您想要的示例电子表格和示例输出?当然,请从他们那里删除您的个人信息。
标签: python-3.x pandas google-sheets-api