0

我正在尝试查询 Adob​​e PDF 服务 API 以从 PDF 文档生成(导出)DOCX。

我刚刚编写了一个 python 代码来生成一个承载令牌,以便从Adob​​e PDF 服务中识别(请参阅此处的问题:https ://stackoverflow.com/questions/68351955/tunning-a-post-request-to-reach- adobe-pdf-services-using-python-and-a-rest-api)。然后我编写了以下代码,我尝试按照此页面中有关EXPORTAdob​​e PDF 服务选项的说明进行操作(此处:https ://documentcloud.adobe.com/document-services/index.html#post-exportPDF )。

这是一段代码:

import requests
import json
from requests.structures import CaseInsensitiveDict
N/B:我没有编写生成令牌和启用服务器识别的代码部分
>> 这部分是通过表单参数上传我的 PDF 文件的 POST 请求
URL = "https://cpf-ue1.adobe.io/ops/:create?respondWith=%257B%2522reltype%2522%253A%2520%2522http%253A%252F%252Fns.adobe.com%252Frel%252Fprimary%2522%257D"

headers = CaseInsensitiveDict()
headers["x-api-key"] = "client_id"
headers["Authorization"] = "Bearer MYREALLYLONGTOKENIGOT"
headers["Content-Type"] = "application/json"

myfile = {"file":open("absolute_path_to_the_pdf_file/input.pdf", "rb")}

j="""
{
  "cpf:engine": {
    "repo:assetId": "urn:aaid:cpf:Service-26c7fda2890b44ad9a82714682e35888"
  },
  "cpf:inputs": {
    "params": {
      "cpf:inline": {
        "targetFormat": "docx"
      }
    },
    "documentIn": {
      "dc:format": "application/pdf",
      "cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/trs_pdf_file_copy.pdf"
    }
  },
  "cpf:outputs": {
    "documentOut": {
      "dc:format": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/output.docx"
    }
  }
}"""

resp = requests.post(url=URL, headers=headers, json=json.dumps(j), files=myfile)
   

print(resp.text)
print(resp.status_code)

代码的状态是400 我已经被服务器很好地验证了但是我得到以下结果print(resp.text)

{"requestId":"the_request_id","type":"Bad Request","title":"Not a multipart request. Aborting.","status":400,"report":"{\"error_code\":\"INVALID_MULTIPART_REQUEST\"}"}

我认为我无法理解 Adob​​e 指南中关于 API 的 EXPORT 作业的 POST 方法的“表单参数”(https://documentcloud.adobe.com/document-services/index.html)。

你有什么改进的想法吗?谢谢你 !

4

2 回答 2

2

j首先将您作为 python变量,dict然后从中创建一个 JSON 字符串。从 Adob​​e 的文档中也不太清楚的是documentIn.cpf:location需要的值与用于文件的密钥相同。我已InputFile0在您的脚本中更正了这一点。还猜测你想保存你的文件,所以我也添加了。

import requests
import json
import time

URL = "https://cpf-ue1.adobe.io/ops/:create?respondWith=%257B%2522reltype%2522%253A%2520%2522http%253A%252F%252Fns.adobe.com%252Frel%252Fprimary%2522%257D"

headers = {
    'Authorization': f'Bearer {token}',
    'Accept': 'application/json, text/plain, */*',
    'x-api-key': client_id,
    'Prefer': "respond-async,wait=0",
}

myfile = {"InputFile0":open("absolute_path_to_the_pdf_file/input.pdf", "rb")}

j={
  "cpf:engine": {
    "repo:assetId": "urn:aaid:cpf:Service-26c7fda2890b44ad9a82714682e35888"
  },
  "cpf:inputs": {
    "params": {
      "cpf:inline": {
        "targetFormat": "docx"
      }
    },
    "documentIn": {
      "dc:format": "application/pdf",
      "cpf:location": "InputFile0"
    }
  },
  "cpf:outputs": {
    "documentOut": {
      "dc:format": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "cpf:location": "C:/Users/a-bensghir/Downloads/P_D_F/output.docx"
    }
  }
}

body = {"contentAnalyzerRequests": json.dumps(j)}

resp = requests.post(url=URL, headers=headers, data=body, files=myfile)
   

print(resp.text)
print(resp.status_code)

poll = True
while poll:
    new_request = requests.get(resp.headers['location'], headers=headers)
    if new_request.status_code == 200:
        open('test.docx', 'wb').write(new_request.content)
        poll = False
    else:
        time.sleep(5)
于 2021-07-13T21:00:44.653 回答
1

我不知道为什么 docx 文件(顺便说一句创建得很好)没有打开,通过弹出窗口告诉内容不可读。也许是由于'wb'解析方法

我遇到过同样的问题。将请求内容类型转换为“字节”解决了它。

poll = True
    while poll:
        new_request = requests.get(resp.headers['location'], headers=headers)
        if new_request.status_code == 200:
            with open('test.docx', 'wb') as f:
                f.write(bytes(new_request.content))
            poll = False
        else:
            time.sleep(5)
于 2021-12-05T23:55:02.263 回答