【发布时间】:2019-02-15 01:33:52
【问题描述】:
我正在尝试使用 python 3.6.5 用我的机器的当前日期/时间更新 google 工作表中的单元格。我正在使用 gspread 连接到谷歌表。
如果我执行以下操作,它将给出我希望放入谷歌表格的日期/时间:
import datetime
print(datetime.datetime.now())
2018-09-10 10:50:38.171795
我正在努力弄清楚如何将该输出打印到谷歌表格中的单元格中。这是我的设置,最后一行是我想要弄清楚的:
import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
#connect to google sheets through API
json_key = 'work.json'
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)
client = gspread.authorize(credentials)
#define googlesheet
rsheet = client.open_by_url('https://docs.google.com/spreadsheets/d/randomgooglesheetid')
#define the correct worksheet, index - 0 is the first sheet, 1 is the second...
engwsr = rsheet.get_worksheet(0)
engwsr.update_acell('O1' , print(datetime.datetime.now()))
最后一行只是将日期时间打印到 python 中,并没有更新谷歌表中的任何内容。我的目标是将“2018-09-10 10:50:38.171795”打印到单元格 O1 中。
有没有更好的方法来做到这一点?日期时间格式不必完全一样,只需使用日期和时间即可轻松读取。
【问题讨论】:
标签: python python-3.x timestamp gspread