蟒蛇+谷歌驱动器:上传xlsx,转换为谷歌表,获得可共享的链接 [英] python + google drive: upload xlsx, convert to google sheet, get sharable link
问题描述
我想要的程序的流程是:
- 将xlsx电子表格上传到驱动器(它是使用pandas
to_excel
) - 将其转换为Google表格格式
- 指定任何具有链接 li>
- 获取链接并与要输入信息的人分享
- 下载完成的工作表
我正在使用PyDrive,它解决了步骤1和步骤5,但还有一些未解决的问题。
如何转换为谷歌表格格式?我试图在创建要用PyDrive上传的文件时将mimeType指定为'application / vnd.google-apps.spreadsheet'
,但那给了我一个错误。
我如何设置文件可以被任何有链接的人编辑?一旦设置完成,我可以使用PyDrive轻松获得共享链接。
更新:使用 convert = True
标志可轻松将xlsx转换为Google表格。见下文。我仍然在寻找一种方法将我的新文件的共享设置设置为任何可以编辑链接的人。
from pydrive.auth导入GoogleAuth
从pydrive.drive导入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})
对于INSERT和COPY方法,都有一个convert的可选查询参数;
$ b $ b
convert = true,
是否将此文件转换为相应的Google文档格式。 (默认值:false)
这里有个python例子:
您需要使用Python客户端库才能使用代码。
来自apiclient导入错误
from apiclient.http import MediaFileUpload
#...
$ b def insert_file(服务,标题,描述,parent_id,mime_type,文件名):
插入新文件
Args:
服务:Drive API服务实例
title:要插入的文件的标题,包括扩展名
description:要插入的文件的描述。
parent_id:父文件夹的ID
mime_type:要插入的文件的MIME类型
filename:要插入的文件的文件名
返回:
插入的文件元数据如果成功,否则无。
media_body = MediaFileUpload(fi lename,mimetype = mime_type,resumable = True)
body = {
'title':title,
'description':description,
'mimeType':mime_type
}
#设置父文件夹。
if parent_id:
body ['parents'] = [{'id':parent_id}]
try:
file = service.files()。insert (
body = body,
convert = true,
media_body = media_body).execute()
#取消注释以下行以打印文件ID
#print'文件ID:%s'%file ['id']
返回文件
except errors.HttpError,错误:
print'发生错误:%s '%error
return None
我没有试过这个,所以你需要来测试它。
The flow of my desired program is:
- Upload an xlsx spreadsheet to drive (it was created using pandas
to_excel
) - Convert it to Google Sheets format
- Specify that it is editable by anyone with the link
- Get the link and share it with someone who will enter information
- Download the completed sheet
I am currently using PyDrive, which solves steps 1 and 5, but there are a few unsolved problems.
How can I convert to google sheets format? I tried to just specify the mimeType as 'application/vnd.google-apps.spreadsheet'
when I created the file to upload with PyDrive, but that gave me an error.
How can I set the file to be editable by anyone with the link? Once that is set, I can get the sharing link easily enough with PyDrive.
UPDATE: conversion from xlsx to google sheets is easy with a convert=True
flag. See below. I am still seeking a way to set the sharing settings of my new file to "anyone with the link can edit".
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})
There is an Optional query parameter of "convert", for both the "INSERT" and "COPY" method;
convert=true,
Whether to convert this file to the corresponding Google Docs format. (Default: false)
There is a python example here:
You need to use the Python client library for the code to work.
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
I haven't tried this, so you'll need to test it.
这篇关于蟒蛇+谷歌驱动器:上传xlsx,转换为谷歌表,获得可共享的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!