我使用PyDrive在Google中创建文件,但是我在实际的Google类型项上遇到了问题。
我的代码是:
file = drive.CreateFile({'title': pagename,
"parents": [{"id": folder_id}],
"mimeType": "application/vnd.google-apps.document"})
file.SetContentString("Hello World")
file.Upload()如果我将mimetype更改为text/plain,这会很好,但它会给我带来错误:
引发ApiRequestError(错误) pydrive.files.ApiRequestError:https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json返回“无效mime类型提供的”>“
如果我将MimeType保留为原样,但删除对SetContentString的调用,也会很好地工作,因此这两件事似乎不能很好地结合在一起。
创建Google并设置内容的正确方法是什么?
发布于 2017-07-31 09:13:07
Mime类型必须与上载的文件格式匹配。您需要一个支持的格式的文件,并且需要以匹配的内容类型上传它。因此,要么:
file = drive.CreateFile({'title': 'TestFile.txt', 'mimeType': 'text/plan'})
file.SetContentString("Hello World")
file.Upload()此文件可通过访问。或,
file = drive.CreateFile({'title': 'TestFile.doc',
'mimeType': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'})
file.SetContentFile("TestFile.docx")
file.Upload()可以用Google打开。支持的格式列表和相应的mime类型可以找到这里。
若要动态地将文件转换为Google格式,请使用:
file.Upload(param={'convert': True})https://stackoverflow.com/questions/45407743
复制相似问题