如何使用 python 3 和 PyPDF2(或任何其他方式)将 pdf 转换为 .docx 文件?

How to turn a pdf into a .docx file using python 3 and PyPDF2 (or any other way)?

我想将 .pdf 转换为 .docx 文件。我尝试了几种方法,但这是最好的方法(如果我错了请纠正我)。我看过这个 ,但它对我不起作用 - 它与此相同:

import PyPDF2

path=r"C:\Users\name\Desktop\test maker tester\Computer Science4838-2020-specimen-paper-1.pdf"
text=""
pdf_file = open(path, 'rb')
text =""
read_pdf = PyPDF2.PdfFileReader(pdf_file)
c = read_pdf.numPages
for i in range(c):
    page = read_pdf.getPage(i)
    text+=(page.extractText())

没有报错,但是找不到Word文档,PDF还在...

您知道如何解决这个问题,或者可以建议任何其他方法将 .pdf 转换为 .docx 文件吗?

python 中没有将 pdf 无缝转换为 docx 的直接方法或软件包。您尝试的方法会将 pdf 转换为 docx,但文档的所有格式都将被删除,您只会在转换后的 docx 中获得没有样式的纯文本。

我亲自尝试了 Adobe 的文档云 SDK 到 python,它通过保留 pdf 文档的原始本机格式将 pdf 转换为 docx。转换每个文档大约需要 15 秒。您可以使用以下链接找到有关如何开始使用的更多信息:

https://github.com/adobe/dc-view-sdk-samples

https://www.adobe.io/apis/documentcloud/dcsdk/docs.html

关于通过python使用此服务的问题,您必须使用subprocess或os.system命令来调用此服务的命令行命令。

更新:

您可以在此处找到有关此方法实现的详细说明 Link。虽然这是针对 OCR 转换,但将 pdf 转换为 docx 的过程完全相同。