【问题标题】:Convert PDF to any with Google Drive API使用 Google Drive API 将 PDF 转换为任何格式
【发布时间】:2013-07-04 15:07:04
【问题描述】:

我正在尝试将 PDF 转换为任何允许的格式,但在上传 pdf 文件后响应不包含 exportLinks。

我已经尝试了很多方法来获取exportLinks。其他文档格式可以上传导出成功。我使用了 developer.google.com 的代码。

media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=False)
body = {'title': title,
        'description': description,
        'mimeType': mime_type,
        }
# Set the parent folder.
if parent_id:
    body['parents'] = [{'id': parent_id}]

try:
    request = service.files().insert(body=body, media_body=media_body)
    #request.uri = "%s&convert=true&ocr=true" % request.uri
    #request.uri = "%s&convert=true" % request.uri
    request.uri = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json&convert=true"
    logger.debug("Request: %s", request.uri)
    file_obj = request.execute()
    # Uncomment the following line to print the File ID
    # print 'File ID: %s' % file_obj['id']
    import json
    logger.debug('Get file result: %s' % json.dumps(file_obj, indent=4))
    return file_obj

...
export_links = source_file.get('exportLinks')
if not export_links:
    logger.debug('Warning: exportLinks does not exist')

为什么 PDF 上传的响应中没有 exportLinks?

请 Google 团队同意 Drive API 可以上传和导出 PDF。

谢谢

[更新] 这是 Httplib2 的请求转储。正如我所见,查询已根据手册正确设置。

Request: https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json&convert=true
send: 'POST /upload/drive/v2/files?uploadType=multipart&alt=json&convert=true HTTP/1.1\r\n
Host: www.googleapis.com\r\n
content-length: 4425\r\n
accept-encoding: gzip, deflate\r\n
accept: application/json\r\n
user-agent: google-api-python-client/1.0\r\n
content-type: multipart/related; boundary="===============0758269123680398078=="\r\n
authorization: Bearer ya29.AHES6ZRDT0AJX8a5JN_u_0epF6ZCMGcqwyru1wzj22oVlMO9CfmFzlo\r\n
\r\n
--===============0758269123680398078==\nContent-Type: application/json\nMIME-Version: 1.0\n\n{"mimeType": "application/pdf", "description": "", "title": "Short_pdf.pdf"}\n--===============

这是响应中的下载​​链接: "downloadUrl": "https://doc-0c-2c-docs.googleusercontent.com/docs/securesc/2sgoib42bclb5coaj85g5574fd2b65bn/2lnqk989jdaepp4b8igdco1j8fu7m9l3/1373040000000/00639208221381605991/00639208221381605991/0By108D1dgCDkX0NFOXV0V2pNd2s?h=16653014193614665626&e=download&gd=true",

【问题讨论】:

  • 响应是否包含下载链接?如果是这样,则没有发生转换。另外,请通过将参数传递给插入方法来设置查询参数:.insert(body=b, media_body=m, convert=True)
  • 我已经更新了帖子。请检查请求有什么问题。另外,我尝试按照您的建议传递转换参数 - 没有成功。请求标头与更新中的相同。我想你的意思是下载链接是“downloadUrl”。谢谢

标签: python api pdf google-drive-api google-drive-realtime-api


【解决方案1】:

很遗憾,Google Drive 不提供通过 Google Drive API 转换 PDF 文件的功能。

只有 Google 原生文档(Google Docs/Sheets/Slides/Drawings)具有exportLinks,允许以多种导出格式下载 Google 文档。所有其他文件 - 基本上是常规文件,例如 PDF、图像等... - 有一个 downloadUrl 代替,它允许您以原始格式下载文件本身。

【讨论】:

  • 好的,谢谢。伙计们,您应该更新您的手册,而不是人们已经蒸发了大脑。顺便说一句,我还找不到任何下载链接。有downloadUrl(developers.google.com/drive/manage-downloads)。
  • 抱歉其实是downloadUrl,downloadLink是错字:)
  • 您能否告诉我们您将更新文档的哪一部分。该文档提到“下载并导出 Google 文档 - exportLinks”。在这种情况下,Google Doc 的意思是:Google 文档/电子表格/演示文稿/图纸。 PDF 是常规文件,因此不是 Google 文档,因此符合“downloadUrl”的条件。
  • 是的,我可以,最后一个链接是一个。没有任何确切和明确的部分说明问题。 “文件资源包括用于 Google 文档(文档、电子表格、演示文稿、)的各种导出格式的 exportLinks URL。”。什么是?也许是绘图?另一部分是描述为“Google Doc 格式和支持的导出 MIME 类型相互映射如下:”的表。根据此表 - PDF 是 Google Doc 格式并且是绘图。正如你所指出的,这不是正常的。 downloadUrl 正在检索文件本身,而不是 Google Doc。
  • @Nivco google 已经在 v3 中通过 API 添加了 pdf 到 google doc 转换和 ocr 转换的功能。我无法找到如何做到这一点,但由于 files.create() 中有一个 ocrLanguage 参数,我想他们有它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多