I just started using Gspread and am trying to access one of my google docs spreadsheets in my google drive. I followed the instructions and went to Google API console and created a JSON file. When I run this code I get no errors:
import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open("mwsSearch-b3d5d5d9c956.json"))
scope = ["https://spreadsheets.google.com/feeds"]
credentials = SignedJwtAssertionCredentials(json_key['client_email'], bytes(json_key['private_key'], 'utf-8'), scope=scope)
gc = gspread.authorize(credentials)
My next step is to try to open a google spreadsheet. I created a spreadsheet titled "Mike" in the main folder of my Google Drive, but have tried to access it via:
gc.open_by_url("https://docs.google.com/spreadsheets/d/1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA/edit#gid=0")
gc.open("Mike")
gc.open_by_key("1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA")
All three of these return the same error:
gspread.exceptions.SpreadsheetNotFound
I am thinking that maybe the api access is linking to another cloud storage through the project, and not my individual google drive, and that is why it is not accessing it. Could someone with more experience in this please point me in the right direction on what I am doing wrong. All help is appreciated. Thank you.
You'll need to add the email which was created with the JSON key to the spreadsheet you want to access. It will be something like 9876.....#developer.gserviceaccount.com. You'll find it as the "client email" in your JSON file and your credential page.
SignedJwtAssertionCredentials has been deprecated.
Look at http://gspread.readthedocs.org/en/latest/oauth2.html
Go to Google Sheets and share your spreadsheet with an email you have in your json_key['client_email']. Otherwise you’ll get a SpreadsheetNotFound exception when trying to open it.
Related
I am using gspread library in my python script to connect to the Google excel sheet and I was wondering if there is a way to also grab the version history via gspread library?
From I am using gspread library in my python script to connect to the Google excel sheet, in your situation, I guessed that Google excel sheet is Google Spreadsheet.
About I was wondering if there is a way to also grab the version history via gspread library?, unfortunately, in the current stage, it seems that gspread has no methods for retrieving the revision list of Spreadsheet. But, when googleapis for python is used, the revision list of the Spreadsheet can be retrieved.
In this answer, I would like to propose to retrieve the revision list of Spreadsheet using the client of gspread. Because, in the recent version of gspread, googlapis can be easily used using the client of gspread. And also, gspread includes the scope for using Drive API. I thought that when the client of gspread is used, it might be useful for your situation. The sample script is as follows.
Sample script:
import gspread
from googleapiclient.discovery import build
client = gspread.oauth(
credentials_filename="###", # Please set your file.
authorized_user_filename="###", # Please set your file.
)
spreadsheetId = "###" # Please set your Spreadsheet ID.
service = build("drive", "v3", credentials=client.auth)
revisions = service.revisions().list(fileId=spreadsheetId).execute()
print(revisions)
When this script is run, the revision list can be retrieved from the Spreadsheet.
Note:
For example, when you want to access the data of the specific version, I thought that the following threads might be useful.
Google Drive API V3: get the content of a revision
How to get older versions of Google Spreadsheet data?
Revert Revision of an Excel File - Drive API
Reference:
Revisions: list
I am trying to build a google sheets based chat with python and I'm having trouble understanding how to read&write from the spread sheet on my drive (without using Google API of course, explanation why at the end*)
So far I've gotten to a place where I can get the file, but I cant read the content. Like so:
import pandas as pd
import requests
from io import StringIO
orig_url='https://docs.google.com/spreadsheets/d/1bnCDl1DqRLqO8xHx3sjWdkydYC7rEb3vjpXUZ3ps2tY/edit?usp=sharing'
file_id = orig_url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url = requests.get(dwn_url).text
csv_raw = StringIO(url)
dfs = pd.read_csv(csv_raw)
print(dfs.head())
P.S. I looked online for many other resources, from I can tell they are all using google API
*I am building the chat app as part of a course and using API's is not a part of it yet, for that reason I cannot use google API
Please refer to the Google api documentation specifically Google sheets API: https://developers.google.com/sheets/api
There you can read about how to enable the sheets api, create authentication tokens and how to use the api calls in your preferred language.
If you don't want to use Google API, you can try sheetdb.io. It's a tool that turns a Google Spreadsheet into a JSON API. So you can use HTTP requests to read and write to a Google Spreadsheet.
I am working on a machine learning task and have saved a Keras model and want to deploy it to Github (so that I can host a web demo using Streamlit and/or Flask). However, the model file is so large (> 1 GB), that I cannot upload it to Github for free.
My thought process regarding an alternative is to upload it to a cloud service such as google drive (or dropbox, box etc.) then using some sort of Python module to access it from there.
So my question is, can I upload a pickle file containing a pickled Keras model to Google Drive and then access that object from a Python script? If so, how would I go about doing so?
Thank you!
I believe you can, you'll need to pip oauth2client & gspread. To access the data you would need to enable API manager on your google drive and get credentials in the form of a json file. Then you would need to share the file with the email in the credentials giving it permission. You could then port over the information as you needed to, I'm not sure how Keras works but this would be the first step.
Another important factor is that Google api is very touch when it comes to requests that are coming to fast, to overcome this put in sleep commands between each one, but if you do that this method may become way to slow for your idea.
scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("Your json file here.json", scope)
client = gspread.authorize(creds)
sheet = client.open("your google sheets name or whatever").sheet1 # Open the spreadhseet
data = sheet.get_all_records() # you can call all the information with this.
I understand that you require a way to upload and download large files* from Drive using Python. If I understood your situation correctly, then you can achieve your goals easily by using the Drive API as #TimothyChen commented. First I highly recommend you to follow the Drive API Python Quickstart tutorial to create a working example. Later, you could modify it to use Files.create() and Files.get() to upload/download files as needed. Don't hesitate to ask me more questions if you have doubts.
*Please, keep in mind that there is a 5 TB size limit in Drive.
Here's my setup and what I want to accomplish:
I currently have a Google Form set up for users to submit information, which is then logged in a Google Sheet.
I then, use PyDrive to save the contents of the Google Sheet as a csv.
Pandas then reads that csv, and deletes data within the csv based on certain criteria.
After that, I use PyDrive to re-upload the "fixed" csv to Google sheets, saving over the old one.
Here's the problem:
whenever I do this, it terminates the link between the Google Sheet and Google Form. Is there some way I can preserve this link or re-link the Form and the Sheet?
Here is the snippet of code I have been playing with:
submissions_data = drive.CreateFile({'id': ext['SUBMISSIONS_ID']})
submissions_data.GetContentFile("data.csv", mimetype = "text/csv")
df = pd.read_csv("data.csv")
df.drop([0],inplace=True)
df.to_csv("data.csv", index=False)
submissions_data.SetContentFile("data.csv")`
Preferably, I would like to find a way to do this using PyDrive, but anything will work at this point. Any help is appreciated! Thank you!
Use Google Drive API with oauth2client and gspread to update the existing spreadsheet directly.
Go to the Google APIs Console. Create a new project. Click Enable
API.
Enable the Google Drive API. Create credentials
for a Web Server to access Application Data.
Name the service account and grant it a Project Role of Editor.
Download the JSON file.
Copy the JSON file to your code directory and rename it to
client_secret.json
Find the client_email inside client_secret.json. Back in your spreadsheet, click the Share button in the top right, and paste the client email into the People field to give it edit rights. Hit Send.
pip install gspread oauth2client
#spreadsheet.py
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Copy of Legislators 2017").sheet1
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
You can write to the spreadsheet by changing a specific cell:
sheet.update_cell(1, 1, "I just wrote to a spreadsheet using Python!")
Or you can insert a row in the spreadsheet:
row = ["I'm","inserting","a","row","into","a,","Spreadsheet","with","Python"]
index = 1
sheet.insert_row(row, index)
Check the gspread API reference for the full details on these functions along with a few dozen others.
I'm trying to read a bunch of spreadsheets within my organization that have been shared with my account and import the records into a database. I'd like to use OAuth2.0 instead of just using raw text passwords, but I'm having trouble getting this to work. My code right now is this:
scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
credentials = SignedJwtAssertionCredentials('5***2#developer.gserviceaccount.com',SIGNED_KEY,scope)
gc = gspread.authorize(credentials)
print gc.openall()
This returns:
$ python read_sheets.py
[]
My account probably has a hundred spreadsheets shared with it, so I'm not sure why this is happening. One idea I had is that the service account email address doesn't have the sheets shared with it, but when I switch to the actual email address of the account, I get oauth2client.client.AccessTokenRefreshError: invalid_grant as an error. It seems like maybe I'm missing a step, but I've followed both the gspread instructions and the google oauth2 for service accounts and don't see what else I need to do. Appreciate the help