【问题标题】:Pydrive: export doc as htmlPydrive:将文档导出为 html
【发布时间】:2016-05-27 12:11:56
【问题描述】:

我正在尝试将 .doc 文件作为 html 从 Google Drive 导出到。这是我的代码。我在文档中没有看到有关如何将文档下载为 html 的任何内容。但这是我的代码与示例相去甚远。我不确定docsfile 指的是什么。

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)


test='https://docs.google.com/document/d/116rpW5DxfVDFTfJeCh3pFl9K8gtuZV5gX035631SKjm8/edit'
docsfile.GetContentFile(test, mimetype='text/html')

【问题讨论】:

  • 这是你的全部代码吗?如果在没有指定 docsfile 是什么的情况下运行,我会感到惊讶。
  • 这是作为示例发布的内容。它显然没有运行。这是链接github.com/googledrive/PyDrive

标签: python pydrive


【解决方案1】:

首先docsfile 是您要导出的文件,在您的情况下是已经在Google Drive 中的.doc 文件。

docsfile = drive.CreateFile({'id': <ID of your file>)

您可以看到更多关于如何下载文件here。这里有完整的文档http://pythonhosted.org/PyDrive/

或者,您可以使用 Google 提供的 python client 直接将文件导出为 html:

    response = service.files().export(
        fileId=fileid, mimeType='text/html'
    ).execute()
    if response:
        with open(<full_path_of_your_destination_html_file>, 'wb') as fh:
            fh.write(response)
    else:
        <handle error here>

service 类似于:

    store = oauth2client.file.Storage(<path_to_your_credentials>)
    credentials = store.get()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http) 

查看有关如何使用 google 客户端 here 的完整示例。

请注意,您在 Google 云端硬盘中的文件必须是 Google 文档 (application/vnd.google-apps.document),而不是文档文件 (application/msword),因此您应该确保文件作为有效的 Google 文档上传.

【讨论】:

  • 这在使用它的地方看起来不像是有效的 Python:mimeType=mimetype='text/html'。在 Python 中,归因是语句而不是表达式,并且不能将语句用作函数调用的参数。
  • 这是一个错字@PauloScardine。更新了回复。谢谢
猜你喜欢
  • 2013-02-05
  • 2023-01-18
  • 2013-06-26
  • 2023-03-31
  • 1970-01-01
  • 2018-04-15
  • 2015-10-23
  • 2014-03-19
  • 1970-01-01
相关资源
最近更新 更多