March 19, 2020 Python Q&A

Topics Covered in Session:

  • Setting up Authentication Credentials needed for securely reading/writing Google sheets

The program in this video did not run successfully. The solution was to go to the URL listed in the error code to enable the Sheets API (only the Drive API had been enabled).

df2gspread_Test.py

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 12 08:13:50 2020

@author: Bruce1
"""

from df2gspread import df2gspread as d2g

import pandas as pd

d = [pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
    pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])]
df = pd.DataFrame(d)

# use full path to spreadsheet file
spreadsheet = 'df2gs_Test'
# or spreadsheet file id
# spreadsheet = '1cIOgi90...'

wks_name = 'Sheet1'

d2g.upload(df, spreadsheet, wks_name)
# if spreadsheet already exists, all data of provided worksheet(or first as default)
# will be replaced with data of given DataFrame, make sure that this is what you need!


from df2gspread import gspread2df as g2d

# use full path to spreadsheet file
spreadsheet = 'df2gs_Test'
# or spreadsheet file id
# spreadsheet = '1cIOgi90...'
wks_name = 'Sheet1'

df2 = g2d.download(spreadsheet, wks_name, col_names = True, row_names = True)

Error:

ipdb>
gspread.exceptions.APIError: {'code': 403, 'message': 'Google Sheets API has not been used in project 567361131022 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=567361131022 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.', 'status': 'PERMISSION_DENIED', 'details': [{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'Google developers console API activation', 'url': 'https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=567361131022'}]}]}

By going to the https://console.developers.google.com/apis/api/sheets.googleapis.com/overview?project=<your project number>, the Google Sheets API can be enabled. The code successfully worked after this was enabled.