【问题标题】:PDF to Word Doc in PythonPython中的PDF到Word文档
【发布时间】:2015-10-22 00:29:03
【问题描述】:

我已经阅读了有关此问题的其他堆栈溢出问题,但它没有回答我的问题,因此请投反对票。它的版本 2.7。

我想要做的就是使用 python 将 PDF 转换为 Word 文档。至少转换为文本,以便我可以复制并粘贴到 word 文档中。

这是我到目前为止的代码。它打印的只是女性性别符号。

我的代码错了吗?我接近这个错误吗?某些 PDF 文件不能与 PDFMiner 一起使用吗?除了使用 PyPDF2 或 PDFMiner 之外,您是否知道实现我将 PDF 转换为 Word 的目标的任何其他替代方法?

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = file('Bottom Dec.pdf', 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()

    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text
print convert_pdf_to_txt(1)

【问题讨论】:

  • 你安装了 LibreOffice 吗?如果是这样,请阅读此答案stackoverflow.com/a/26358582/797495
  • 唉,我没有。只是普通的旧 MS Word;并在那个时候过时了...... 2003。它是我的工作,而不是我。不过我确实看到了。
  • "某些 PDF 文件不能与 PDFMiner 一起使用吗?"是的。 '一个总是可以从每个 PDF中正确提取所有文本'不是事实。请发布一个指向您遇到问题的 PDF 的链接,以便我们确定问题出在您的代码、PDFMiner 中,还是根本不包含任何可提取的文本。

标签: python-2.7 pdf ms-word


【解决方案1】:

另一个替代解决方案是Aspose.Words Cloud SDK for Python,您可以从pip 安装它,以便将PDF 转换为DOC。

import asposewordscloud
import asposewordscloud.models.requests
api_client = asposewordscloud.ApiClient()
api_client.configuration.host = 'https://api.aspose.cloud'
# Get AppKey and AppSID from https://dashboard.aspose.cloud/
api_client.configuration.api_key['api_key'] = 'xxxxxxxxxxxxxxxxxxxxx' # Put your appKey here
api_client.configuration.api_key['app_sid'] = 'xxxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx' # Put your appSid here

words_api = asposewordscloud.WordsApi(api_client)
filename = '02_pages.pdf'
remote_name = 'TestPostDocumentSaveAs.pdf'
dest_name = 'TestPostDocumentSaveAs.doc'
#upload PDF file to storage
request_stoarge = asposewordscloud.models.requests.UploadFileRequest(filename,remote_name)
response = words_api.upload_file(request_stoarge)
#Convert PDF to DOC and save to storage
save_options = asposewordscloud.SaveOptionsData(save_format='doc', file_name=dest_name)
request = asposewordscloud.models.requests.SaveAsRequest(remote_name, save_options)
result = words_api.save_as(request)
print("Result {}".format(result))

我是 Aspose 的开发布道者。

【讨论】:

    【解决方案2】:
    from pdf2docx import Converter
    
    pdf_file = 'E:\Muhammad UMER LAR.pdf'
    
    doc_file= 'E:\Lari.docx'
    c=Converter(pdf_file)
    
    c.convert(doc_file)
    c.close()
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2021-02-16
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多