PyDrive:创建Google Doc fi

2024-09-29 01:29:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用PyDrive在googledrive中创建文件,但是我在实际的googledoc类型的项目上遇到了问题。在

我的代码是:

file = drive.CreateFile({'title': pagename, 
"parents":  [{"id": folder_id}], 
"mimeType": "application/vnd.google-apps.document"})

file.SetContentString("Hello World")

file.Upload()

如果我将mimetype更改为text/plain,这很好,但是它会给我错误:

raise ApiRequestError(error) pydrive.files.ApiRequestError: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json returned "Invalid mime type provided">

如果我保持MimeType不变,但是删除对SetContentString的调用,那么这两个功能似乎不能很好地结合在一起。在

创建Google文档并设置内容的正确方法是什么?在


Tags: 文件项目代码id类型titlefilesdrive
1条回答
网友
1楼 · 发布于 2024-09-29 01:29:05

Mime类型必须与上载的文件格式匹配。您需要一个支持格式的文件,并且需要上载具有匹配内容类型的文件。所以,要么:

file = drive.CreateFile({'title': 'TestFile.txt', 'mimeType': 'text/plan'})
file.SetContentString("Hello World")
file.Upload()

这个文件可以通过Google笔记本访问。或者

^{pr2}$

可以用谷歌文档打开。可在here找到受支持格式和相应mime类型的列表。在

要将文件动态转换为Google Docs格式,请使用:

file.Upload(param={'convert': True})

相关问题 更多 >