![](/img/trans.png)
[英]Convert a PDF to DOCX using Adobe PDF Services via REST API (with Python)
[英]Convert a PDF to DOCX using Adobe PDF Services via REST API (with Python in Manjaro) Issues
我正在关注这个问题的答案: Convert a PDF to DOCX using Adobe PDF Services via REST API (with Python)以便能够将 pdf 文档导出为 docx 文档。
我能够成功获取导出的文档数据并将其保存到 docx 文件中。 问题是当我尝试使用 LibreOffice 打开它时,它会显示一条消息:
文件“test.docx”已损坏,因此无法打开。 LibreOffice 可以尝试修复该文件。 损坏可能是由于数据传输造成的文档操作或结构文档损坏的结果。 我们建议您不要相信已修复文档的内容。 本文档禁止执行宏。
LibreOffice 应该修复文件吗?r 文件。*
当我单击“是”时,它抱怨说:
文件“test.docx”无法修复,因此无法打开。
然后关闭 LibreOffice。 我还应该说,我也尝试使用 google docs 和 OneDrive 打开test.docx文件,但都无法打开该文件。 One Drive 显示一条消息,内容如下:
无法打开此文档进行编辑。
在这里,我添加了我正在使用的整个 python 脚本(我用占位符替换了敏感信息):
import requests
import json
import time
url = "https://cpf-ue1.adobe.io/ops/:create?respondWith=%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D"
payload = {
"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": "/path_to_the_output_file/output.docx"
}
}
}
pdf_file = {"InputFile0": open('/path_to_the_pdf_file/mypdf.pdf','rb')}
headers = {
'Authorization': 'Bearer My_bearer_tok',
'Accept': 'application/json, text/plain, */*',
'x-api-key': 'eb78dc9d04e54f10be1fd2189d91f8c9',
'Prefer': 'respond-async,wait=0'
}
body = {"contentAnalyzerRequests": json.dumps(payload)}
response = requests.post(url=url, headers=headers, data=body, files=pdf_file)
print(response.text)
print(response.headers)
print(response.status_code)
time.sleep(5)
poll = True
while poll:
new_request = requests.get(response.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)
在代码的最后一部分,我还尝试了有关如何保存文件的两个建议:
with open('test.docx', 'wb') as f:
f.write(new_request.content)
with open('test.docx', 'wb') as f:
f.write(bytes(new_request.content))
但似乎没有一个工作。 我还想评论一下,我能够使用 Adobe 从他们的网页手动将 pdf 转换为 docx 并使用 LibreOffice 加载它,但我需要能够自动执行此操作。
更新 1:我尝试使用 Microsoft Word 从 Windows 笔记本电脑打开 docx 文件,它还抱怨文档包含无法识别的信息,但最终能够显示 docx 文档。
更新 2:也可以使用 python pdf2docx 包打开生成的 docx。
所以,欢迎任何关于可能发生的事情的想法(也许这是预期的行为),谢谢!
据我所知,您正在“按原样”获取响应并保存它,但响应实际上是一个多部分表单响应。 您必须先对其进行解析,并且其中包含数据的实际位。 多部分响应包括 json 信息 + 二进制文件,这就是您需要解析它的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.