我使用pdfminer将复杂的(表、图)和非常长的pdf转换为html。我想进一步分析结果(例如提取表格、段落等),然后使用nltk中的句子标记器进行进一步分析。为此,我想将html保存为文本文件,以了解如何进行解析。不幸的是,我的代码没有将html写入txt:
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
def convert_pdf_to_html(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(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()
str1 = retstr.getvalue()
retstr.close()
return str1
with open("D:/my_new_file.txt", "wb") as fh:
fh.write(str1)
此外,代码会在shell中打印整个html字符串:如何避免它?
首先,除非有一个微不足道的错误,
.txt写入发生在返回函数之后:txt文件写入从未执行!
然后,要抑制控制台的输出,只需在运行例程之前执行此操作:
import sys,os
oldstdout = sys.stdout # save to be able to restore it later
sys.stdout = os.devnull