Home>

I want to work with Google SpreadSheet in python.

Various settings such as enabling the API of Google Drive and Sheets, sharing the sheet, issuing the private key, etc. have been completed.
I'm writing code to operate Google SpreadSheet with python.
I am stuck in the process of setting the authentication information (service account key) and logging in to the Google API.

! pip install gspread
! pip install oauth2client
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
Get credentials using #json file
SCOPES = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE ='my-project-test-296177.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name (SERVICE_ACCOUNT_FILE, SCOPES)
  217 the keyfile.
    218 "" "
->219 with open (filename,'r') as file_obj:
    220 client_credentials = json.load (file_obj)
    221 return cls._from_parsed_json_keyfile (client_credentials, scopes,
FileNotFoundError: [Errno 2] No such file or directory:'my-project-test-296177.json'

I get an error.
I knew I could enter the downloaded JSON file name.json, but I'm not sure what that was.
If anyone can understand it, I would like you to teach me. Thank you.

  • Answer # 1

    If the json file is in the same folder as the py file you want to execute, you can omit the path specification,
    If it is in a different folder, you need to specify the path.

    SERVICE_ACCOUNT_FILE = r'C: \ Users \ username \ Desktop \ folder name \ my-project-test-296177.json'