【问题标题】:How to convert PDF to clean HTML using Python如何使用 Python 将 PDF 转换为干净的 HTML
【发布时间】:2019-11-25 19:35:00
【问题描述】:

我想让用户上传 PDF,将该 PDF 转换为 HTML 代码,然后将此代码插入到 <div> 中以显示 PDF 文档。我正在使用 PDFMiner 来分析上传的 PDF。当我将其转换为 HTML 时,HTML 很乱,并且文档显示错误HTML Mess .我已经尝试过 XML,但它仍然无法使用,因为文本显示时没有空格。我该如何改进呢?谢谢。


def main():

 contentRaw =  convert_pdf(file.filename, 'html')
 contentR = json.dumps(contentRaw)
 contentOut = (contentRaw)
 return render_template('app.html', title=" App", filename=file.filename, content=Markup(contentOut), instructions=instructions)



def convert_pdf(path, format='text', codec='utf-8', password=''):
    rsrcmgr = PDFResourceManager()
    retstr = BytesIO()
    laparams = LAParams()
    if format == 'text':
        device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    elif format == 'html':
        device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    elif format == 'xml':
        device = XMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    else:
        raise ValueError('provide format, either text, html or xml!')
    fp = open(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    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().decode()
    fp.close()
    device.close()
    retstr.close()
    return text

【问题讨论】:

  • 应该使用诸如 PDF.js 之类的库来显示 PDF,而不是转换为 HTML:mozilla.github.io/pdf.js
  • @MaxiMouse 我想在页面的其他元素中显示 pdf,例如 div。 PDF.js 可以帮助解决这个问题吗?另外,我认为 PDF.js 在网络服务器上运行,我正在为这个项目使用 python。有没有用 Python 编写的类似于 pdf.js 的库?我没有找到任何可靠的东西。
  • PDF.js 在浏览器中运行。它将 PDF 呈现到 canvas 元素上,您可以将其放入 div 中。

标签: python html pdf flask pdfminer


【解决方案1】:

PDF 是一种非常普及的格式,它不仅仅是一种标记语言(HTML 是)。

编写 PDF 到 HTML 的转换器以保留文档的外观是一个相当复杂的故事:您的软件必须理解所有命令、对象、维护图形状态等。执行符合标准的 PDF 阅读器和查看器所做的所有事情。 毕竟将文档内容转换为 HTML。

您可以从PDF 1.7 specification开始。

我建议您查看pdfreader 并编写自定义 PDFViewer 或以某种方式处理它可以提取的文本 + pdf 命令。

【讨论】:

    【解决方案2】:

    为什么不尝试使用现有的 PDF 到 HTML 转换器?使用现有库的示例:

    import pdftables_api
    
    c = pdftables_api.Client('my-api-key')
    c.html('input.pdf', 'output.html')
    

    【讨论】:

      【解决方案3】:

      如果您有兴趣尝试其他 Python 包,那么我建议您使用Aspose.Words Cloud SDK for Python。它支持 PDF 到 HTML 转换中的格式。

      # For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
      # Import module
      import asposewordscloud
      import asposewordscloud.models.requests
      from shutil import copyfile
      
      # Please get your Client ID and Secret from https://dashboard.aspose.cloud.
      client_id='xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
      client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxx'
      
      words_api = asposewordscloud.WordsApi(client_id,client_secret)
      words_api.api_client.configuration.host='https://api.aspose.cloud'
      
      filename = 'C:/Temp/02_pages.pdf'
      dest_name = 'C:/Temp/02_pages.html'
      #Convert RTF to text
      request = asposewordscloud.models.requests.ConvertDocumentRequest(document=open(filename, 'rb'), format='html')
      result = words_api.convert_document(request)
      copyfile(result, dest_name)
      
      
      

      P.S:我是 Aspose 的开发布道者

      【讨论】:

        猜你喜欢
        • 2014-07-25
        • 2012-05-04
        • 1970-01-01
        • 2021-11-14
        • 2011-11-26
        • 1970-01-01
        • 2015-03-25
        • 2010-09-21
        • 2011-12-25
        相关资源
        最近更新 更多