0

I an wondering is there is a way to write to google spreadsheet using Python. Found python-gdata-client library, installed it with all dependencies. Using this code below but it is not working

import time
import gdata.spreadsheet.service

email = '[email protected]'
password = 'pwd'
weight = '180'
# Find this value in the url with 'key=XXX' and copy XXX below
spreadsheet_key = 'pRoiw3us3wh1FyEip46wYtW'
# All spreadsheets have worksheets. I think worksheet #1 by default always
# has a value of 'od6'
worksheet_id = 'Sheet1'

spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()

# Prepare the dictionary to write
dict = {}
dict['date'] = time.strftime('%m/%d/%Y')
dict['time'] = time.strftime('%H:%M:%S')
dict['weight'] = weight
print dict

entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
  print "Insert row succeeded."
else:
  print "Insert row failed."

This is the error is says -

Traceback (most recent call last):
  File "D:/steve/test.py", line 28, in <module>
    entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
  File "C:\Python27\lib\site-packages\gdata\spreadsheet\service.py", line 338, in InsertRow
    converter=gdata.spreadsheet.SpreadsheetsListFromString)
  File "C:\Python27\lib\site-packages\gdata\service.py", line 1235, in Post
    media_source=media_source, converter=converter)
  File "C:\Python27\lib\site-packages\gdata\service.py", line 1346, in PostOrPut
    redirects_remaining - 1, media_source, converter=converter)
  File "C:\Python27\lib\site-packages\gdata\service.py", line 1328, in PostOrPut
    return converter(result_body)
  File "C:\Python27\lib\site-packages\gdata\spreadsheet\__init__.py", line 376, in SpreadsheetsListFromString
    xml_string)
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 92, in optional_warn_function
    return f(*args, **kwargs)
  File "C:\Python27\lib\site-packages\atom\__init__.py", line 126, in CreateClassFromXMLString
    tree = ElementTree.fromstring(xml_string)
  File "<string>", line 124, in XML
ParseError: mismatched tag: line 944, column 4
4
  • 1
    Can you add the full stacktrace? There aren't 944 lines of code here Commented Aug 24, 2015 at 8:37
  • When you ask questions you should always post full tracebacks. Commented Aug 24, 2015 at 8:41
  • Sorry for the I will do it now Commented Aug 24, 2015 at 8:47
  • Hi, I tried all the steps mentioned here - gspread.readthedocs.org/en/latest/oauth2.html . But no success ,, Traceback :Traceback (most recent call last): File "D:\steve\get_oauth2_token.py", line 12, in <module> wks = gc.open("siteprice") File "C:\Python27\lib\site-packages\gspread\client.py", line 150, in open raise SpreadsheetNotFound SpreadsheetNotFound Commented Aug 24, 2015 at 16:17

3 Answers 3

5

You are using ClientLogin method which was deprecated since April 20, 2012. It seems, that Google has turned it off on 26 of May, 2015.

Use OAuth2 instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Alik, Do you how to do that, I am bit new to API programming
@ShekharSamanta I gave you a specific link with examples in Python. Read it and try to implement
Hi, I tried all the steps mentioned here - gspread.readthedocs.org/en/latest/oauth2.html . But no success ,, Traceback :Traceback (most recent call last): File "D:\steve\get_oauth2_token.py", line 12, in <module> wks = gc.open("siteprice") File "C:\Python27\lib\site-packages\gspread\client.py", line 150, in open raise SpreadsheetNotFound SpreadsheetNotFound
@ShekharSamanta so the code works. Make sure siteprice spreadsheet exists.
0

as Alik mentioned above starting from 2015 u need to use oauth2client which you can install by running:

pip install oauth2client

here is a sample code to login:

import gspread
from oauth2client.service_account import ServiceAccountCredentials


# trying to log in 
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope) # u can download the json file from google's api manager 
client = gspread.authorize(creds) # authorize access

# defining the sheet that we will work on
sheet = client.open('sheetName').sheet1  # getting a sheet to work on 

I know its been a year now since you asked your question, but i hope i helped some one else.

Comments

-1

Yes, this is possible using a few packages: gspread, google-api-python-client, oauth2client, tweepy

...for writing a dataframe, use: gspread_dataframe

pip install gspread, google-api-python-client, oauth2client, tweepy, gspread_dataframe

I wrote a script for tracking twitter sentiment here on GitHub.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.