使用Python编写google电子表格

使用Python编写google电子表格,python,api,python-2.7,google-docs,google-docs-api,Python,Api,Python 2.7,Google Docs,Google Docs Api,我想知道是否有一种方法可以使用Python编写google电子表格。找到库,并将其与所有依赖项一起安装。正在使用下面的代码,但它不起作用 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_ke

我想知道是否有一种方法可以使用Python编写google电子表格。找到库,并将其与所有依赖项一起安装。正在使用下面的代码,但它不起作用

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
回溯(最近一次呼叫最后一次):
文件“D:/steve/test.py”,第28行,在
entry=spr\u client.InsertRow(dict、电子表格键、工作表id)
文件“C:\Python27\lib\site packages\gdata\spreadsheet\service.py”,第338行,插入行
转换器=gdata.spreadsheet.SpreadsheetsListFromString)
文件“C:\Python27\lib\site packages\gdata\service.py”,第1235行,在Post中
媒体源=媒体源,转换器=转换器)
PostOrPut中第1346行的文件“C:\Python27\lib\site packages\gdata\service.py”
重定向\u剩余-1,媒体\u源,转换器=转换器)
PostOrPut中的文件“C:\Python27\lib\site packages\gdata\service.py”,第1328行
返回转换器(结果_体)
文件“C:\Python27\lib\site packages\gdata\spreadsheet\\uuuu init\uuuuu.py”,第376行,位于电子表格SlistFromString中
xml(字符串)
文件“C:\Python27\lib\site packages\atom\\uuuuu init\uuuuuu.py”,第92行,在可选的\u warn\u函数中
返回f(*args,**kwargs)
CreateClassFromXMLString中的文件“C:\Python27\lib\site packages\atom\\uuuuu init\uuuuuu.py”,第126行
tree=ElementTree.fromstring(xml\u字符串)
文件“”,第124行,XML格式
ParseError:不匹配的标记:第944行第4列
您正在使用的,自2012年4月20日以来已被弃用。谷歌似乎在2015年5月26日关闭了它

请改用。

您正在使用的是自2012年4月20日以来就已弃用的。谷歌似乎在2015年5月26日关闭了它


改为使用。

如上所述,从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 

我知道你问问题已经一年了,但我希望我能帮助其他人

如上所述,从2015年开始,Alik需要使用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 

我知道你问问题已经一年了,但我希望我能帮助其他人

是的,这可以通过几个软件包实现:gspread、googleapi python客户端、oauth2client、tweepy

…要写入数据帧,请使用:gspread_dataframe

pip安装gspread、google api python客户端、oauth2client、tweepy、gspread_数据框架


我写了一个跟踪推特情绪的脚本。

是的,这可以通过几个软件包实现:gspread、googleapi python客户端、oauth2client、tweepy

…要写入数据帧,请使用:gspread_dataframe

pip安装gspread、google api python客户端、oauth2client、tweepy、gspread_数据框架


我写了一个跟踪推特情绪的脚本。

你能添加完整的stacktrace吗?这里没有944行代码当你提问时,你应该始终发布完整的回溯。很抱歉,我现在就做。嗨,我尝试了这里提到的所有步骤-gspread.readthedocs.org/en/latest/oauth2.html。但是没有成功,回溯:回溯(最后一次调用):文件“D:\steve\get\u oauth2\u token.py”,第12行,wks=gc.open(“siteprice”)文件“C:\Python27\lib\site packages\gspread\client.py”,第150行,在open-raise-SpreadsheetNotFound-SpreadsheetNotFound-speadsheetnotfound中,您可以添加完整的堆栈跟踪吗?这里没有944行代码当你提问时,你应该始终发布完整的回溯。很抱歉,我现在就做。嗨,我尝试了这里提到的所有步骤-gspread.readthedocs.org/en/latest/oauth2.html。但是没有成功,回溯:回溯(最后一次调用):文件“D:\steve\get\u oauth2\u token.py”,第12行,wks=gc.open(“siteprice”)文件“C:\Python27\lib\site packages\gspread\client.py”,第150行,在open raise SpreadsheetNotFound SpreadsheetNotFoundHi Alik中,你怎么做,我对API有点陌生programming@ShekharSamanta我为您提供了一个Python示例的特定链接。阅读并尝试实现嗨,我尝试了这里提到的所有步骤-。但是没有成功,回溯:回溯(最后一次调用):文件“D:\steve\get\u oauth2\u token.py”,第12行,wks=gc.open(“siteprice”)文件“C:\Python27\lib\site packages\gspread\client.py”,第150行,在open-raise-SpreadsheetNotFound中SpreadsheetNotFound@ShekharSamanta所以代码是有效的。确保
siteprice
电子表格存在。嗨,Alik,你知道怎么做吗,我对API有点陌生programming@ShekharSamanta我为您提供了一个Python示例的特定链接。阅读并尝试实现嗨,我尝试了这里提到的所有步骤-。但是没有成功,回溯:回溯(最后一次调用):文件“D:\steve\get\u oauth2\u token.py”,第12行,wks=gc.open(“siteprice”)文件“C:\Python27\lib\site packages\gspread\client.py”,第150行,在open-raise-SpreadsheetNotFound中SpreadsheetNotFound@ShekharSamanta所以代码是有效的。确保
siteprice
电子表格存在。