python - 使用 Google Drive API 将 PDF 转换为任何格式

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

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

我已经尝试过很多方法来设法获得 exportLinks。其他文档格式可以上传导出成功。我使用了来自 developers.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 ",

最佳答案

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

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

关于python - 使用 Google Drive API 将 PDF 转换为任何格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473558/

相关文章:

python - 安装包后出现 'module not found' 错误

ruby-on-rails - Ruby Google API key 和分析

c# - PDF 签名 - 嵌入单独签名的哈希

java - 在 JAVA 中将 PDF 打印为字节数组

javascript - 在 JavaScript 页面上编辑可填写的 PDF 文件

python - 如果,if-else和Elif语句

python - 如何创建 Perl 代码的 Python 包装器?

python - Sqlalchemy使用orm选择所有连接不存在的mysql

ruby-on-rails - 尽管 API 有效,但 Swagger-ui 只发送 OPTIONS 而不是 POST http 方法

api - firebase-admin - 身份验证错误 :TypeError: URL is not a constructor