Dump data from python cron job to google spreadsheets - python

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!

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

Version History - Google Excel using gspread

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

How to save a googlesheet chart as image by Python

I'm using Googlesheet API with Python and I can get access to the sheet and the cells now. However, I don't know how to get the chart in the sheet.
client = gspread.service_account_from_dict(creds)
workbook = client.open('HR - 8/16-8/31 Data')
sheet = workbook.get_worksheet(0)
H1 = sheet.acell('B3').value
I found this question:How to download charts in PNG from google sheet mentioned I can use the getCharts() function, but it is for JavaScript only. If there a similar function in Python?
Currently the API doesn't have a method to do this. The charts overview documentation explains how to manipulate and create them, but not how to export them. Reading the data also only gives you a JSON representation of it, not an image. It seems that the Apps Script getCharts() leverages other server-side functions that are not in the regular API.
This is documented as a feature request in Google's issue tracker here, so you can +1 it if you want. In that thread a possible workaround was posted. If you publish your file you can build a URL if you know the chartID to generate it as an image:
https://docs.google.com/spreadsheets/d/e/<publish-id>/pubchart?oid=<chart-id>&format=image
Gspread doesn't seem to have methods to do this so you'll have to use the Google APIs. In their Python Quickstart you can find a sample to set up authorization, and you can use spreadsheets.get(), which gives you all the data from the spreadsheet including the chart IDs. If you only have a single chart that you want to export periodically then you can just get the ID once from the UI and just retrieve it with Python. The caveat is that you have to publish the Sheet which you don't want to do with sensitive information.
As another alternative you could build an Apps Script Web App which uses the getCharts() method in the answer that you linked, and just send a POST message from your Python app and have Apps Script return the image in its response.

Google Drive API python to export each worksheet of spreadsheet separately in csv format

I am referring to below Google Drive api to export Google spreadsheet as CSV.
https://developers.google.com/drive/v2/web/manage-downloads
Its working fine But as mentioned in guide it downloads only 1st sheet into csv format. I am looking for a way to download all worksheets into csv format separately.
gdata library of python is not working after OAuth1 has has been deprecated.
Please suggest if someone has done it successfully in OAuth2.
Drive API itself does not offer a way to export a specific worksheet, but with a valid bearer token you can just download it in the desired format via its Google Drive url. The pattern is https://docs.google.com/spreadsheets/d/{{document_id}}/export?format=tsv&gid={{gid}}. The document_id and gid are visiable in the browser URL bar when you open the sheet in Google Drive. Replace tsv with whatever format you need.
I'm not good enough in Python, but I created a demo app in Node.js: git#github.com:joerx/drive-exporter.git. The general flow is:
Obtain an access token via the Google APIs OAuth2 flow
Figure out sheet_id and gid
Construct download url
Make a regular HTTP request passing the token as Authorization: Bearer {{token}}
For public sheets you can skip the authorisation part.
General documentation for using Drive API and OAuth2 (with examples in Python) is here
If you need to programmatically determine the gid, this might help: How to convert Google spreadsheet's worksheet string id to integer index (GID)?

Take data from private google spreadsheet

I have a Google Spreadsheet which I'm sharing with several person. I want to built a script to search for some rows and take cells values and process a program locally afterwards. I was thinking going with python, as it seems Google provide a good API for it.
Have someone an example on how to connect to Google Spreadsheet ? I read the api, but I don't get how does the OAuth 2.0 thing works...
Many thanks :)
To perform read and write operations on a Google Spreadsheet, OAuth 2.0 will not be necessary. As shown in these samples for reading as well as writing to a Google Spreadsheet, in order to be able to access the SpreadSheet you must include the username-password of the related account in your in your code. And then using them in the following manner (as shown in the writing sample) to access the Spreadsheet:
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
Also, do remember to import gdata.spreadsheet.service and any other related library that you might need for your code. And if you're new, this would also be a good place to start. Hope this helps!

Categories

Resources