(2020 年 10 月):都2020年了,这个问题确实过时了。谷歌文档列表API https://support.google.com/code/answer/74212 was 已于 2012 年弃用 https://developers.googleblog.com/2012/09/retiring-google-documents-list-api-v3.html and 2015年停产 https://gsuiteupdates.googleblog.com/2015/03/final-reminder-deprecated-google-apps.html,替换为谷歌云端硬盘API https://developers.google.com/drive.
Drive API 能够导入(上传和转换)Word 文件,无论是创建图像还是将图像更新为 Google Docs 格式,都不会出现问题。下面是这两种情况的一些伪代码(Python)。第一个上传名为的Word文件person.docx
其中有一个图像。上传后,会向用户显示一条消息,您可以验证图像是否在 Google 文档中。
DOCX_FILE = 'person.docx'
DOCS_MIME = 'application/vnd.google-apps.document'
:
: # credentials code for user acct auth (OAuth client ID) or service acct auth
:
DRIVE = discovery.build('drive', 'v3', ...) # http= or creds= dep on auth type
body = {'name': DOCX_FILE, 'mimeType': DOCS_MIME}
res = DRIVE.files().create(media_body=DOCX_FILE, body=body,
fields='name,mimeType').execute()
print('Uploaded "%s" (as %s)' % (res['name'], res['mimeType']))
正如OP提到的,创建文件和以前一样完美。如果您编辑现有文档,除了像以前一样提供文件有效负载之外,您还需要提供其云端硬盘文件 ID,并调用files().update()
代替files().create()
,它也有效:
DRIVE_ID = 'YOUR_FILE_ID'. # existing file in Drive
DOCX_FILE = 'person.docx' # Word file to replace the above file with
DOCS_MIME = 'application/vnd.google-apps.document'
:
: # credentials code as above
:
DRIVE = discovery.build('drive', 'v3', ...) # same as above
body = {'name': DOCX_FILE, 'mimeType': DOCS_MIME}
res = DRIVE.files().update(fileId=DRIVE_ID, media_body=DOCX_FILE,
body=body, fields='name,mimeType').execute()
print('Updated "%s" (as %s)' % (res['name'], res['mimeType']))
如果您不熟悉 Drive API,请继续阅读。用于操作已经存在的文档in开车,具体来说面向文档的操作,您可以使用 Google Docs、Sheets 和 Slides API,但要执行file-level access例如导入/导出、复制、移动、重命名等,使用谷歌云端硬盘API http://developers.google.com/drive反而。如果您是 Drive API 的新手,除了上述内容之外,这里还有一些示例:
-
并深入研究代码post http://wescpy.blogspot.com/2014/11/authorized-google-api-access-from-python.html-- 如果您不熟悉上面提到的授权代码,这很有用
-
Google Drive:上传和下载文件 http://googledevelopers.blogspot.com/2016/01/google-drive-uploading-downloading.html加上“穷人的纯文本到 PDF 转换器”代码深入研究post http://wescpy.blogspot.com/2015/12/google-drive-uploading-downloading.html (*)
- 将 Google 表格导出为 CSV 博客post http://wescpy.blogspot.com/2016/07/exporting-google-sheet--as-csv.html only
(*) - TL;DR:将纯文本文件上传到云端硬盘,导入/转换为 Google 文档格式,然后将该文档导出为 PDF。上面的帖子使用 Drive API v2;这个后续帖子 http://wescpy.blogspot.com/2015/12/migrating-to-new-google-drive-api-v3.html描述了将其迁移到 Drive API v3,这是开发者视频 http://googledevelopers.blogspot.com/2016/01/google-drive-uploading-downloading.html结合两个“穷人的转换器”帖子。
要了解有关如何将 Google API 与 Python 结合使用的更多信息,请查看my blog http://wescpy.blogspot.com以及各种 Google 开发者视频 (series 1 http://youtube.com/playlist?list=PLOU2XLYxmsILOIxBRPPhgYbuSslr50KVq and series 2 http://youtube.com/playlist?list=PLOU2XLYxmsIJJFx_MVCQJ7eWF3gDxkl)我已经制作了。