使用Python写入谷歌电子表格

时间:2015-08-24 08:34:25

标签: python api python-2.7 google-docs google-docs-api

我想知道是否有办法使用Python写入谷歌电子表格。找到python-gdata-client库,将其与所有依赖项一起安装。使用下面的代码,但它无法正常工作

import time
import gdata.spreadsheet.service

email = 'email@gmail.com'
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."

这是错误 -

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

3 个答案:

答案 0 :(得分:5)

您使用的ClientLogin method自2012年4月20日起已被弃用。看来Google已于2015年5月26日将其关闭。

改为使用OAuth2

答案 1 :(得分:0)

正如Alik从2015年开始提到的,你需要使用oauth2client,你可以通过运行来安装:

pip install oauth2client

这是一个登录示例代码:

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 

我知道自从你提出问题以来已经过了一年,但我希望我帮助其他人。

答案 2 :(得分:0)

是的,可以使用一些软件包来做到这一点:gspread,google-api-python-client,oauth2client,tweepy

...用于编写数据框,请使用:gspread_dataframe

  

pip安装gspread,google-api-python-client,oauth2client,tweepy,gspread_dataframe

我写了一个脚本来跟踪推特情绪here on GitHub