djs*_*sei 8 python google-sheets google-drive-api pydrive
我想要的程序的流程是:
to_excel)我目前正在使用PyDrive,它解决了第1步和第5步,但是有一些未解决的问题.
我怎样才能转换为谷歌表格式?我试图将mimeType指定为'application/vnd.google-apps.spreadsheet'当我使用PyDrive创建要上传的文件时,但这给了我一个错误.
如何将文件设置为可由具有该链接的任何人编辑?一旦设置完毕,我就可以轻松地使用PyDrive获得共享链接.
更新:使用convert=True标志很容易从xlsx转换为Google表格.见下文.我仍然在寻找一种方法来将我的新文件的共享设置设置为"具有该链接的任何人都可以编辑".
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
test_file = drive.CreateFile({'title': 'testfile.xlsx'})
test_file.SetContentFile('testfile.xlsx')
test_file.Upload({'convert': True})
Run Code Online (Sandbox Code Playgroud)
对于“INSERT”和“COPY”方法都有一个可选的查询参数“convert”;
convert=true,
Run Code Online (Sandbox Code Playgroud)
是否将此文件转换为相应的 Google Docs 格式。(默认值:假)
这里有一个 python 示例:
您需要使用 Python 客户端库才能使代码正常工作。
from apiclient import errors
from apiclient.http import MediaFileUpload
# ...
def insert_file(service, title, description, parent_id, mime_type, filename):
"""Insert new file.
Args:
service: Drive API service instance.
title: Title of the file to insert, including the extension.
description: Description of the file to insert.
parent_id: Parent folder's ID.
mime_type: MIME type of the file to insert.
filename: Filename of the file to insert.
Returns:
Inserted file metadata if successful, None otherwise.
"""
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
try:
file = service.files().insert(
body=body,
convert=true,
media_body=media_body).execute()
# Uncomment the following line to print the File ID
# print 'File ID: %s' % file['id']
return file
except errors.HttpError, error:
print 'An error occured: %s' % error
return None
Run Code Online (Sandbox Code Playgroud)
我还没有尝试过这个,所以你需要测试一下。
| 归档时间: |
|
| 查看次数: |
4160 次 |
| 最近记录: |