gspread.exception Spreadsheet not Found on a particular sheet - python

I am working to get google sheets downloaded on my EC2 linux server.I shared the google sheet with email id from credentials json file. I am using gspread, to open the sheet using open_by_key(file['id']) and the code works when I run using root but not using a different user having the same properties. It gives this error :
File "/home/edh/.local/lib/python2.7/site-packages/gspread/client.py",
line 113, in open_by_key
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
This code works in team drives too. The weird thing is I am able to run this code over different google sheets in the same user of EC2, but this particular sheet gives the above error.
Please help and let me know if you need any more info from my side.

Related

Sharepoint API how to read file havin only sharing link to it

I'm using python office365 library to access sharepoint documents. I don't know how to access file via API that have been shared with me by sharing link. I need to get this file content and if possible metadata (last modify date). Could anyone help?
The user that I'm using have no access to this sharepoint folder other than a sharing link to a single file.
I tried many variations of normal file access API, bot by hand and by office365 library. I couldnt find a way to access a file when I have only sharing link to it.
My sharing link looks like that:
https://[redacted].sharepoint.com/:x:/s/[redacted]/dir1/dir2/ESd0HkNNSbJMhQFavQsr9-4BNHC2rHSWsnbs3zRdjtZsC3g so there is not really a filename here and I cannot read via API content of any folder per se because I have an error Attempted to perform an unathorized operation.. Authentication goes fine (when i mistake password I get different error).
According to my research and testing, you can use the following Rest API to read file (get file content):
https://xxxx.sharepoint.com/sites/xxx/_api/web/GetFolderByServerRelativeUrl('/sites/xxx/Library_Name/Folder Name')/Files('Document.docx')/$value
If you want to get last modify date, you can use the following Rest API to get the Modified field:
https://xxxx.sharepoint.com/sites/xxx/_api/web/lists/getbytitle('test_library')/Items?$select=Modified

How to read file from Google Drive after it was updated?

I need to place .csv file somewhere and then update it on daily basis for code to read it. The situation is the person who will be updating file will not be using coding, and it should be as easy as to upload from web for him. I read tens of question here about how to read or download file from Google Drive, Google storage but all of them suppose updating with code or downloading using API. I want little bit simpler solution. For example,
I'm using the code below to read the .csv file from google drive (this file is, for example, actual will be different).
This is the file which will be updated each day, however each time I update file (remove and upload new one into google drive) the link changes.
Is there a way to update file every time but not making change to code?
For example to make code to get file from the particular folder on Google Drive?
The main thing is that I need to do it without using API and Google Oauth.
If it's not possible where could the file be uploaded for this purpose? I need to be able to upload file every day without any code so code will read update data. Is there storage like this?
import pandas as pd
import requests
from io import StringIO
url='https://drive.google.com/file/d/1976F_8WzIxj9wJXjNyN_uD8Lrl_XtpIf/view?usp=sharing'
file_id = url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url2 = requests.get(dwn_url).text
csv_raw = StringIO(url2)
df = pd.read_csv(csv_raw)
print(df.head())
create vs update
The first thing you need to be sure is that the first time you run your code you use the file.create method.
However when you updated the file you should be using file.update this should not be creating a new file each time. There by your id will remain the same.
google api python client
IMO you should consider using the python client library this will make things a little easer for you
updated_file = service.files().update(
fileId=file_id,
body=file,
newRevision=new_revision,
media_body=media_body).execute()
Google sheets api
You are editing a csv file. By using the google drive api you are downloading the file and uploading it over and over.
Have you consider converting it to a google sheet and using the google sheet api to edit the file programmatically. May save you some processing.

Export sharepoint list data to excel using python

I have sharepoint url where i can open and export that sharepoint list into excel. I want to automate that using python. i have tried many suggestions from online. but none of those are working.
i have tried below code to connect sharepoint
import requests
url= 'my sharepoint file path here'
r=requests.get(url,verify=False) #no username or password required
i am expecting the code should export sharepoint list data to new excel file.
I haven’t done this myself with code, but I believe it should work similar to what’s been done in SharePoint 2013: Export To Excel Using REST API
What they do there is actually using a link to a query that Excel can understand with parameters as the list id and view id. Since they use code within the browser it recognizes the content type (.iqy file) as something that should be opened in Excel. Don’t know how that would be when dont in Python though.
Another thing: Unless you have anonymous access turned on in your site, you need to authenticate the request to SharePoint.

Dump data from python cron job to google spreadsheets

I am creating a CSV which contains the report as a result of a cronjob. I want to share this CSV via Google spreadsheets - the report itself is versioned, so I would just dump the CSV contents into the same worksheet in the same spreadsheet each and every single time.
I have found gspread which looked very promising but unfortunately gives me NoValidUrlKeyFound Errors. The Python example for interacting with the Spreadsheets API v4 (to be found here) requires interactivity because of the OAuth flow.
Can someone point me in the right direction? Ideally I would just do:
client = spreadsheet.client(credential_file)
client.open_spreadheet(url_or_something).open_worksheet(0).dump(csv)
print("Done")
I have found the answer in a Github issue:
Using gspread, follow the official guide to receive an oauth token. You will end up with a JSON file holding the credentials.
In order to access a spreadsheet with these credentials however, you need to share the spreadsheet with the account represented by them. In the JSON file there is a property client_email. Share the document with that e-mail address and voila, you'll be able to open it!

Google Drive API insert Excel file works, but update doesn't

I'm using the Google Drive API and have found that I can access a Google spreadsheet's metadata and download it in 'xls' and 'xlsx' formats just fine. I've also been able to use the API to insert (add new) Excel files and convert them to Google spreadsheets. However, no matter what I do, I get 500 errors trying to update a Google Spreadsheet with an Excel file. Is this unsupported?
I'm using the python client library and examples for insert and upload:
https://developers.google.com/drive/v2/reference/files/insert#examples
https://developers.google.com/drive/v2/reference/files/update#examples
I have a single script where auth happens fine, I build a service just fine, and with the example code above modified only to add the parameter convert=True I can call:
insert_file(service, title="test", description="description", parent_id=None,
mime_type="application/vnd.ms-excel", filename="test.xls")
Success. I can open this new spreadsheet on Google Drive, which has been converted.
variable file_id below is set to the file_id returned in the JSON from the successful file creation. I can use this id to get the new file's metadata again, download the file, etc.
then
update_file(service, file_id, title="modified test",
description="modified description",
mime_type="application/vnd.ms-excel", filename="test2.xls",
new_revision=True)
Fails with error 500
I've tried with new_revision=False, not changing any of the metadata, and setting resumable=False in the MediaFileUpload() object creation.

Categories

Resources