python - Pdf Miner 返回奇怪的字母/字符

标签 python python-3.x pdf text pdfminer

我正在使用带有 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 替换为 fi:

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

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

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

以及 Alphabetic Presentation block 中的其他一些内容,您也可以将其包含在内。

(*) 不要错误地更改 æaeœoe。这些不是“纯粹的打印连字”,而是它们本身的有效字符。

关于python - Pdf Miner 返回奇怪的字母/字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52863575/

相关文章:

python-3.x - 将 MongoDb 同步到 ElasticSearch

使用ReportLab创建Python/PDF-带有图案的自制网格打印,但在屏幕上看起来不错

python - Conda (Python) 虚拟环境不能从 Windows 移植到 Linux

python - 排序双向链表 Python

python - 如何在python中获取property的setter方法的属性

macos - 如何为 Mac 安装 python3.4-dev?

java - iText - Java Android - 将字段添加到现有 pdf

javascript - PDF.js 字体加载期间出错

python - 在前夕使用复合键作为附加端点?

python - 存储一组四个(或更多)值的最佳数据结构是什么?