【问题标题】:python + google drive: upload xlsx, convert to google sheet, get sharable linkpython + google drive:上传xlsx,转换为google sheet,获取共享链接
【发布时间】:2015-02-27 01:48:07
【问题描述】:

我想要的程序的流程是:

  1. 上传 xlsx 电子表格到驱动器(它是使用 pandas to_excel 创建的)
  2. 将其转换为 Google 表格格式
  3. 指定任何人都可以通过链接编辑它
  4. 获取链接并与将输入信息的人分享
  5. 下载完成的工作表

我目前正在使用 PyDrive,它解决了第 1 步和第 5 步,但还有一些未解决的问题。

如何转换为谷歌表格格式?当我创建要使用 PyDrive 上传的文件时,我尝试将 mimeType 指定为 'application/vnd.google-apps.spreadsheet',但这给了我一个错误。

如何将文件设置为任何知道链接的人都可以编辑?一旦设置好,我就可以使用 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})

【问题讨论】:

  • 我不确定这是否是最佳答案,但如果一切都失败了,您可以调用一个应用程序脚本应用程序来执行您想要的操作。您需要运行 HTTP 请求。 Access - Permission
  • 感谢您提出这个问题。你有没有想过如何使用 pydrive 来广告权限?

标签: python google-drive-api google-sheets pydrive


【解决方案1】:

对于“INSERT”和“COPY”方法都有一个可选的“convert”查询参数;

convert=true,

是否将此文件转换为相应的 Google Docs 格式。 (默认:假)

这里有一个python示例:

Google Documentation - Copy

您需要使用 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

这个我没试过,所以你需要测试一下。

【讨论】:

  • 谢谢!在 PyDrive 中成功地将 convert=True 标志作为输入参数添加到 Upload() 方法。请参阅上面我的问题中的更新代码。
【解决方案2】:

为了将文件设置为对任何拥有链接的人都可编辑,您必须插入包含以下信息的新权限:

from apiclient import errors
# ...

def share_with_anyone(service, file_id):
  """Shares the file with anyone with the link

  Args:
    service: Drive API service instance.
    file_id: ID of the file to insert permission for.

  Returns:
    The inserted permission if successful, None otherwise.
  """
  new_permission = {
      'type': "anyone",
      'role': "writer",
      'withLink': True
  }
  try:
    return service.permissions().insert(
        fileId=file_id, body=new_permission).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

然后获取您要访问的链接:file["alternateLink"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多