我正在使用带有 python 3 的 pdfminer,并且我在从 pdf 恢复的文本中收到奇怪的字母。
例如,我得到significant
而不是significant
(请注意,字母f
和I
合并了合为一)。
我不知道为什么会发生这种情况。这是我正在使用的代码。
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
from nltk.tokenize import sent_tokenize
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, '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()
sentences = sent_tokenize(text)
for s in sentences:
print(s)
print("\n\n")
到目前为止我唯一的猜测是它可能与编码有关,但似乎 there is no way to retrieve the encoding of a pdf
最佳答案
PDFminer 工作正常。有问题的字符是 Unicode 字符 U+FB01,即 fi
ligature .
在您的代码中添加一行以将 fi
替换为 fi
:
for s in sentences:
s = s.replace ('fi', 'fi')
print (s)
Unicode 中定义了另一种非常常见且纯粹是打印 (*) 的连字:U+FB02,fl
连字;对待这个同样:
s = s.replace ('fl', 'fl')
以及 Alphabetic Presentation block 中的其他一些内容,您也可以将其包含在内。
关于python - Pdf Miner 返回奇怪的字母/字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52863575/