Pydrive:将文档导出为 html
Pydrive: export doc as html
我正在尝试将 .doc 文件导出为 html 从 Google 驱动器进入。这是我的代码。我在文档中没有看到任何有关如何将文档下载为 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
是您要导出的文件,在您的情况下,.doc
文件已经在 Google 驱动器中。
docsfile = drive.CreateFile({'id': <ID of your file>)
您可以查看更多有关如何下载文件的信息here. Here the full documentation http://pythonhosted.org/PyDrive/
或者,您可以使用 python client Google 直接将文件导出为 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 Drive 中的文件必须是 Google Doc (application/vnd.google-apps.document
),而不是 doc 文件 (application/msword
),所以您应该确保该文件是作为有效 Google 文档上传的