PDFminer - 有没有办法从 pdfminer 将 pdf 转换为 html?

问题描述

是一种使用pdfminer 将pdf 转换为html 的简单方法吗? 我见过很多这样的问题,但他们不会给我正确的答案...

我已在 ConEmu 提示中输入:

# pdf2txt.py -o output.html -t html sample.pdf
usage: C:\Program Files\python37-32\Scripts\pdf2txt.py [-P password] [-o output] [-t text|html|xml|tag] [-O output_dir] [-c encoding] [-s scale] [-R rotation] [-Y normal|loose|exact] [-p pagenos] [-m maxpages] [-S] [-C] [-n] [-A] [-V] [-M char_margin] [-L line_margin] [-W word_margin] [-F Boxes_flow] [-d] input.pdf ...

我希望这不是我应该从 pdf2txt.py 得到的回应..

有没有可以工作的代码片段? 我试过这个:

from pdfminer.pdfinterp import PDFResourceManager,pdfpageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import pdfpage
from io import BytesIO


def convert_pdf_to_html(path):
    rsrcmgr = PDFResourceManager()
    retstr = BytesIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = HTMLConverter(rsrcmgr,retstr,codec=codec,laparams=laparams)
    fp = open(path,'rb')
    interpreter = pdfpageInterpreter(rsrcmgr,device)
    password = ""
    maxpages = 0 #is for all
    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)
    fp.close()
    device.close()
    str = retstr.getvalue()
    retstr.close()
    return str

test = convert_pdf_to_html('E://sample.pdf')

但它没有给我任何 html 文件或任何输出

和另一个代码

import pdfminer
from pdfminer.pdfinterp import PDFResourceManager,process_pdf
from pdfminer.converter import HTMLConverter,TextConverter
from pdfminer.layout import LAParams
rsrcmgr = PDFResourceManager()
laparams = LAParams()
converter = HTMLConverter if format == 'html' else TextConverter
device = converter(rsrcmgr,out_file,codec='utf-8',laparams=laparams)
process_pdf(rsrcmgr,device,in_file,pagenos=[1,3,5],maxpages=9)

with contextlib.closing(tempfile.NamedTemporaryFile(mode='r',suffix='.xml')) as xmlin:
    cmd = 'pdftohtml -xml -nodrm -zoom 1.5 -enc UTF-8 -noframes "%s" "%s"' % (
            pdf_filename,xmlin.name.rpartition('.')[0])
    os.system(cmd + " >/dev/null 2>&1")
    result = xmlin.read().decode('utf-8')

它给出了这个:

Traceback (most recent call last):

  File "E:\Blah\blah\blah.py",line 2,in <module>
    from pdfminer.pdfinterp import PDFResourceManager,process_pdf

ImportError: cannot import name 'process_pdf' from 'pdfminer.pdfinterp' (c:\program files\python37-32\lib\site-packages\pdfminer\pdfinterp.py)

信息:

System : Windows 7 SP-1 32-bit
Python : 3.7.0
PDFminer : 20191125

解决方法

安装 pdfminer 并使用以下命令将 pdf 转换为 html

$ pip3 install pdfminer
$ pdf2txt.py  -o output.html document.pdf
,

关于带有 ImportError: cannot import name 'process_pdf' from 'pdfminer.pdfinterp' 的第二个代码片段,我建议检查 this GitHub issue

显然 process_pdf() 已被 PDFPage.get_pages() 取代。功能几乎相同(使用您使用的参数(rsrcmgr,device,in_file,pagenos=[1,3,5],maxpages=9)它可以工作!)因此请检查现场实施情况。

,

你的第一个代码片段很好。 但不要使用 retstr = BytesIO() 使用 retstr = StringIO()

我认为您阅读了使用 BytesIO() 的 Pdfminer 旧文档,在新版本 (v20191125) 中您必须使用 StringIO()

此外,不必在 HTMLConverter() 中传递编解码器值。因此,将 HTMLConverter 更改为:

HTMLConverter(rsrcmgr,retstr,laparams=laparams) 

这些改变对我有用。

希望他们也能为您工作。