python - 使用 Python,如何从 Google 文档中读取纯文本?

标签 python download google-docs google-docs-api

我正在尝试从 Python 脚本中读取 Google 文档的原始文本/内容(只是一个普通文档,而不是电子表格或演示文稿),但到目前为止收效甚微。

这是我尝试过的:

import gdata.docs.service
client = gdata.docs.service.DocsService()
client.ClientLogin('email', 'password')
q = gdata.docs.service.DocumentQuery()
q.AddNamedFolder('email', 'Folder Name')
feed = client.Query(q.ToUri())
doc = feed.entry[0] # extract one of the documents

然而,这个类型为gdata.docs.DocumentListEntry的变量doc似乎并不包含任何内容,只是关于文档的元信息。

我是不是做错了什么?有人能指出我正确的方向吗?谢谢!

最佳答案

更新(2019 年 3 月)好消息! Google Docs REST API现在可用。更多信息来自 my SO answer to a similar question , 但为了让你继续,这里是 official Python "quickstart" sample向您展示如何以纯文本形式获取 Google 文档的标题。

最初在下面回答的 Apps Script 和 Drive REST API 解决方案仍然有效,并且是获取 Google 文档内容的替代方法。 (Drive API 适用于 Python 2 和 3,但 Apps 脚本仅适用于 JavaScript。)

底线:如果您想以纯文本形式下载整个文档,Drive API 解决方案是最佳选择。如果您想以编程方式对文档的不同部分进行 CRUD,那么您必须使用 Docs API 或 Apps 脚本。

(2017 年 2 月)OP 中的代码和唯一的其他答案现在都已过时 ClientLogin authentication was deprecated回到 2012 年(!),和 GData APIs是上一代的 Google API。虽然并非所有 GData API 都已弃用,但 all newer Google APIs 使用the Google Data protocol .

尽管 Google Apps Script 提供了“类 API”服务,但(目前)没有可用于 Google 文档文档的 REST API。 ,云中的 JavaScript 解决方案,提供对 Google 文档的编程访问(通过其 DocumentService 对象),包括 Docs add-ons .

要从 Google 文档中读取纯文本,被认为是文件访问权限,您可以使用 Google Drive API反而。使用 Drive API 的示例:

  • 将 Google 表格导出为 CSV (blog post)
  • “穷人的纯文本到 PDF”转换器 ( blog post ) (*)

(*) - TL;DR:将纯文本文件上传到云端硬盘,导入/转换为 Google 文档格式,然后将该文档导出为 PDF。上面的帖子使用 Drive API v2; this follow-up post描述了将其迁移到 Drive API v3,这里是一个 developer video合并两个“穷人的皈依者”帖子。

OP 的解决方案是执行与您在上面两篇文章中看到的类似的操作,但确保您使用的是 text/plain 导出 MIME 类型。对于 Drive 的其他导入/导出格式,请参阅 this related question SO answer以及 downloading files from Drive docs page .下面是一些伪代码,用于在我的云端硬盘文件夹中搜索名为“Hello World”的 Google Docs 文档,并在屏幕上显示找到的第一个匹配文件的内容(假设 DRIVE 是您的 API 服务端点):

from __future__ import print_function

NAME = 'Hello World'
MIME = 'text/plain'

# using Drive API v3; if using v2, change 'pageSize' to 'maxResults',
# 'name=' to 'title=', and ".get('files')" to ".get('items')"
res = DRIVE.files().list(q="name='%s'" % NAME, pageSize=1).execute().get('files')
if res:
    fileID = res[0]['id']  # 1st matching "Hello World" name
    res = DRIVE.files().export(fileId=fileID, mimeType=MIME).execute()
    if res:
        print(res.decode('utf-8')) # decode bytes for Py3; NOP for Py2

如果您需要的不止这些,请观看这​​些视频以了解如何 setup using Google APIs , OAuth2 authorization , 和 creating a Drive service endpoint to list your Drive files , 加上 corresponding blog post for all three .

要了解有关如何将 Google API 与 Python 一般结合使用的更多信息,请查看 my blog以及我正在制作的各种 Google 开发者视频(series 1series 2)。

关于python - 使用 Python,如何从 Google 文档中读取纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726409/

相关文章:

python - 在 Airflow 2.0 中运行多个 Athena 查询

javascript - node/express 强制浏览器使用自定义名称下载文件

javascript - 使用 javascript 将图像客户端从网站下载到硬盘

javascript - 使用 Apps 脚本检索 Google 文档中特定文本字符串的超链接

javascript - 使用 Google Apps 脚本从 Google 文档中的文本中检索链接的 URL

google-apps-script - 如何在保留段落每个单词格式的列表项中插入段落对象?

python - 在 Python 中找到最匹配的 block /补丁

python - 在 Atom 上运行 Python 代码时获取 "EOFError"

python - 如何在 GridSearchCV 中评估分类器的准确性并生成留一的 roc 曲线?

amazon-web-services - 从 AWS Elastic Beanstalk 下载应用程序