python - 通过 REST API(使用 Python)使用 Adobe PDF 服务将 PDF 转换为 DOCX
问题描述
我正在尝试查询 Adobe PDF 服务 API 以从 PDF 文档生成(导出)DOCX。
我刚刚编写了一个 python 代码来生成一个承载令牌,以便从Adobe PDF 服务中识别(请参阅此处的问题:https ://stackoverflow.com/questions/68351955/tunning-a-post-request-to-reach- adobe-pdf-services-using-python-and-a-rest-api)。然后我编写了以下代码,我尝试按照此页面中有关EXPORT
Adobe 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\"}"}
我认为我无法理解 Adobe 指南中关于 API 的 EXPORT 作业的 POST 方法的“表单参数”(https://documentcloud.adobe.com/document-services/index.html)。
你有什么改进的想法吗?谢谢你 !
解决方案
j
首先将您作为 python变量,dict
然后从中创建一个 JSON 字符串。从 Adobe 的文档中也不太清楚的是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)
推荐阅读
- python - 如何制作 DataFrame 的曲面图?
- node.js - Node.js firebase 集成
- c++ - 如何解决 make-file 的 target 'run failed 错误的配方?
- google-cloud-platform - f1-micro 中的 GCLOUD Kubernetes 导致(f1-micro 机器的节点池由于内存不足不支持)
- python-2.7 - 使用 scapy 获取 DNS 数据包的原始负载
- javascript - 如何在 selenium-python 的 javascript 中处理 webelement 的 id - 而不是属性 id(!)?
- redirect - 想要从 URL 中删除语言,例如将 abc.com\en\about 重定向到 abc.com\about
- javascript - 如何在summernote文本编辑器中禁用右键单击
- gstreamer - 无法从 gstreamer 管道流式传输 hls 内容
- apache-commons - BeanUtils filter Set 基于对象属性和值比较