【发布时间】:2012-09-10 13:06:34
【问题描述】:
(为清楚起见,这篇文章涉及到 Google Documents List API 和 Google Drive API 与 Python 在 Google App Engine 上的区别)
使用 [现已弃用] 文档列表 API,我可以通过导出为 HTML、修改 HTML 然后重新上传来编辑 Google 文档,无论是作为新文档还是作为对原始文档的修改。这对于从模板生成 PDF 文档等事情很有用。我一直在尝试使用新的 Drive API (V2) 复制此功能,但似乎无法做到。
想出了这个...
http = # authenticated http client
drive_service = build('drive', 'v2', http=http)
# get the file ...
file = drive_service.files().get(fileId=FILE_ID).execute()
# download the html ...
url = file['exportLinks']['text/html']
response, content = http.request(url)
# edit html
html = content.replace('foo', 'bar')
# create new file with modified html ...
body = {'title':'Modified Doc',
'description':'Some description',
'mimeType':'text/html'}
media_body = MediaIoBaseUpload(StringIO.StringIO(html), 'text/html', resumable=False)
drive_service.files().insert(body=body, media_body=media_body)
上述代码将 html 文件作为文件上传到 Google Drive,而不是将 HTML 呈现为 Google Document。很公平,这是有道理的。但是,如何像使用 Documents List API 那样将其呈现为 Google Doc?
另一件事 - 如果我设置 resumable=True 它会在 App Engine 上引发以下错误 - '_StreamSlice' 没有 len()。不知道如何让 resumable=True 工作?
最后一件事 - sample code in the docs 使用 MediaInMemoryUpload 对象,但 if you look at the source 现在已弃用,取而代之的是 MediaIoBaseUpload。示例代码应该更新吗?!
【问题讨论】:
标签: python google-app-engine google-drive-api google-api-python-client