How to add data in next line of GoogleSheet using Python - python

I am fetching daily order book and profit/loss data using broker API and feeding them using python with the help of Gspread library.
Following code, I am using to feed fetched order book data from broker into google sheet.
wkob.update('A2',[obdf.columns.values.tolist()] + obdf.values.tolist())
I have to run this python daily using Windows Schduler but each time I am running python file data has been over written on existing one in Googlesheet. I used following code to fetech existing value of available data on sheet but dont know how to use this value to add fresh data next row of existing one. Please help regarding this.
len(wkpnl.get_all_values())
Auto update fresh value after previously updated data in google sheet using python.
Screenshot of code

In your script, how about the following modification?
From:
wkob.update('A2',[obdf.columns.values.tolist()] + obdf.values.tolist())
To:
wkob.append_rows([obdf.columns.values.tolist()] + obdf.values.tolist(), value_input_option="USER_ENTERED")
By this modification, the values are appended to the sheet.
If you want to put only the values, how about the following modification?
wkob.append_rows(obdf.values.tolist(), value_input_option="USER_ENTERED")
Reference:
append_rows

Related

Python: Issue Updating Data Links In Excel After Python Dataframe Export

Situation
I'm working on a data project integrating python in Google Colab and Excel 365 on Win 8.1. My python code collects new data updates on a regimented schedule and then exports/writes (e.g. overwrites, not appends the data) like to a report on an Excel spreadsheet.
I have no issue getting this to work going to a standalone spreadsheet.
I know I could potentially do all this in Python and not use Excel at all, but I prefer not to reinvent the wheel and not spend hours hardcoding all the formulas and links already existing in Excel.
Goal
My goal is to:
1. Use new data from my python export to populate/overwrite a data table on Sheet A in an existing Excel workbook.
2. Then I have a separate Sheet B in the same Excel workbook performing calculations via pre-existing links connecting to the original data table on Sheet A. I then want the links to auto update each time my python export updates the data table on the first sheet.
Problem
The issues I am running into are that if I use the df.to_excel function to export the data and even if I use the spreadsheet name parameter, the export overrides the data table and names the tab okay, but wipes out any other pre-existing sheets within the same workbook.
So I attempted a work around by exporting to an external workbook and then trying to update the links in the second workbook automatically. Problem is the links don't appear to update without the source data file and the second workbook with the links both being manually opened and then the updated file manually saved.
I tried using openpyxl to control the excel files but it appeared to have no effect on the files and no data was updated. (See code block and result at the end of this post.)
Assistance
Does anybody know a way to use python to:
1. Overwrite a specific sheet within an Excel workbook without wiping out the other existing sheets? And then have the links on another sheet automatically update which are connected to the new data?
Or
2. Auto update external links between separate Excel workbooks while the files are unopened?
Or
3. Control an instance of excel that can open both files to allow the links to auto update and then save and close the files automatically?
I found a post from some years ago that identified a win32 package for python that appeared to be able to control instances of excel. When I try doing a pip install in Colab I got an error that the package was unrecognized or doesn't exist.
Ideally, I would prefer not to use VB if at all possible to solve this.
Any solutions are much appreciated.
Thanks in advance.
Sample Code that isn't producing any results:
import openpyxl
# Example code
from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook('/content/drive/MyDrive/Data/Series/AC5M.xlsx', keep_links=True)
ws = wb.active
Workbook.save
Workbook.close
print(ws)
Result:
"function openpyxl.workbook.workbook.Workbook.close"

Using smartsheet-python-sdk to put data back into Smartsheet

I used the smartsheet-python-sdk (with a unique API key from Smartsheet) to automatically pull data from Smartsheet into my Python script along with other data sources (from Tableau) to create new feature-engineered columns. I now want to put these new columns I've created back into the same Smartsheet file I initially pulled from. Is there an automatic way to put these new columns I created back into the same Smartsheet I initially pulled data from using the smartsheet-python-sdk? Thank you!
Yes, the REST API documentation shows how to do it via the Python API in the examples on the right sidebar.
I find the Python API woefully under-documented, but a) it closely parallels the REST API with a few differences, like parameters being in Pythonic snake_case instead of JS camelCase; and b) the examples are usually enough to get you there.

how to connect to an external API using python?

I am trying to write a script which takes the usernames from an excel sheet in a loop and then connect to an external API of a website and get the user ID's from it and give gave the response in the excel sheet. Please help me with an example code.
I need help on two things:-
1:- How to read a particular column elements from an excel sheet
2:- Write a code in the script which uses an API of a website to feed the excel sheet usernames in it in a loop and retrieve the user ID's
For reading the information from the excel sheet take a look at https://docs.python.org/2/library/csv.html
For retrieving the user IDs it depends on the API itself so you would need to provide more information.
Additionally, you might want to look at this Python library for the Instagram API.
This site contains pointers to the best information available about working with Excel files in the Python programming language.
This site will show you how to use APIs with python.

How to export raw data feeds from Google Big Query to R servers in a CSV tabular format?

I tried using the Python code mentioned in the link https://cloud.google.com/bigquery/exporting-data-from-bigquery
However, on running the code it says that since the table has a nested schema it does not allow it to be exported as a CSV.
My objective is to extract the raw daily feeds from the Big Query environment to local R Server environment in order to do custom statistics on the clickstream.
Please help me out!
Nested or repeated data cannot be exported to CSV. You could instead export your BigQuery table to JSON. The python sample you were following should work, you'll just need to add a destinationFormat to your extract job's configuration, like at https://cloud.google.com/bigquery/exporting-data-from-bigquery#extractconfig
'destinationFormat': 'NEWLINE_DELIMITED_JSON'
Documentation on this parameter: https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.extract.destinationFormat

Batch inserts into spreadsheet using python-gdata

I'm trying to use python-gdata to populate a worksheet in a spreadsheet. The problem is, updating individual cells is woefully slow. (By doing them one at a time, each request takes about 500ms!) Thus, I'm attempting to use the batch mechanism built into gdata to speed things up.
The problem is, I can't seem to insert new cells. I've scoured the web for examples, but I couldn't find any. This is my code, which I've adapted from an example in the documentation. (The documentation does not actually say how to insert cells, but it does show how to update cells. Since this is a new worksheet, it has no cells.)
Furthermore, with debugging enabled I can see that my requests returns HTTP 200 OK.
import time
import gdata.spreadsheet
import gdata.spreadsheet.service
import gdata.spreadsheets.data
email = '<snip>'
password = '<snip>'
spreadsheet_key = '<snip>'
worksheet_id = 'od6'
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
# create a cells feed and batch request
cells = spr_client.GetCellsFeed(spreadsheet_key, worksheet_id)
batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()
# create a cell entry
cell_entry = gdata.spreadsheet.SpreadsheetsCell()
cell_entry.cell = gdata.spreadsheet.Cell(inputValue="foo", text="bar", row='1', col='1')
# add the cell entry to the batch request
batchRequest.AddInsert(cell_entry)
# submit the batch request
updated = spr_client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)
My hunch is that I'm simply misunderstanding the API, and that this should work with changes. Any help is much appreciated.
I recently ran across this as well (when trying to delete) but per the docs here it doesn't appear that batch insert or delete operations are supported:
A number of batch operations can be combined into a single request.
The two types of batch operations supported are query and update.
insert and delete are not supported because the cells feed cannot be
used to insert or delete cells. Remember that the worksheets feed must
be used to do that.
I'm not sure of your use case, but would using the ListFeed help at all? It still won't let you batch operations, so there will be the associated latency, but it may be more tolerable than what you're dealing with now (or were at the time).
As of Google I/O 2016, the latest Google Sheets API supports batch cell updates (and reads). Be aware however, that GData is now deprecated, along with most GData-based APIs, including your sample above as the new API is not GData. Also putting email addresses and passwords in plain text in code is a security risk, so new(er) Google APIs use OAuth2 for authorization. You need to get the latest Google APIs Client Library for Python. It's as easy as pip install -U google-api-python-client [or pip3 for Python 3].
As far as batch insert goes, here's a simple code sample. Assume you have multiple rows of data in rows. To mass-inject this into a Sheet, say with file ID SHEET_ID & starting at the upper-left in cell A1, you'd make one call like this:
SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID, range='A1',
body={'values': rows}, valueInputOption='RAW').execute()
If you want a longer example, see the first video below where those rows are read out of a relational database. For those new to this API, here's one code sample from the official docs to help get you kickstarted. For slightly longer, more "real-world" examples, see these videos & blog posts:
Migrating SQL data to a Sheet plus code deep dive post
Formatting text using the Sheets API plus code deep dive post
Generating slides from spreadsheet data plus code deep dive post
The latest Sheets API provides features not available in older releases, namely giving developers programmatic document-oriented access to a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.)
However, to perform file-level access on Sheets, such as import/export, copy, move, rename, etc., you'd use the Google Drive API. Examples of using the Drive API:
Exporting a Google Sheet as CSV (blogpost)
"Poor man's plain text to PDF" converter (blogpost) (*)
(*) - TL;DR: upload plain text file to Drive, import/convert to Google Docs format, then export that Doc as PDF. Post above uses Drive API v2; this follow-up post describes migrating it to Drive API v3, and here's a developer video combining both "poor man's converter" posts.

Categories

Resources