PDFminer returns 很奇怪 letters/characters

Pdf Miner returns weird letters/characters

我在 python 3 中使用 pdfminer,我在从 pdf 恢复的文本中得到了奇怪的字母。

例如,我得到 significant 而不是 significant(请注意字母 fI 合并为一个)。

我不知道为什么会这样。这是我正在使用的代码。

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

for s in sentences:
    s = s.replace ('fi', 'fi')
    print (s)

Unicode 中定义了另一种非常常见的纯印刷 (*) 连字:U+FB02,fl 连字;同样对待:

    s = s.replace ('fl', 'fl')

Alphabetic Presentation block 中的其他几个,您不妨也包括在内。

(*) 不要 错误地将 æ to ae and œ 更改为 oe。这些 不是 'purely typographic ligatures' 但它们本身是有效字符。