Я вот так читаю таблицы ГД(в импортах может быть лишнее, просто удалил свой код):
from __future__ import print_function
import httplib2
import os
import configparser as cfg
import requests
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_id.json'
APPLICATION_NAME = 'APPLICATION_NAME'
config = cfg.ConfigParser()
config.read('Config.ini', encoding='utf_8') # чтение конфиг файла
GDURL = config['DEFAULT']['GDURL'] # ссылка на ГД
RangeName = config['DEFAULT']['RangeName'] # Название листа в ГД
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'sheets.googleapis.com-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Нужно для Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
requests.packages.urllib3.disable_warnings()
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
spreadsheetId = str(GDURL).split('/')[5]
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheetId, range=RangeName).execute()
values = result.get('values', [])
if not values:
print('Нет данных')
else:
# здесь мой код
if __name__ == '__main__':
main()