python - pyPdf 忽略 PDF 文件中的换行符

标签 python string pdf unicode pypdf

我正在尝试将 PDF 的每一页提取为字符串:

import pyPdf

pages = []
pdf = pyPdf.PdfFileReader(file('g-reg-101.pdf', 'rb'))
for i in range(0, pdf.getNumPages()):
    this_page = pdf.getPage(i).extractText() + "\n"
    this_page = " ".join(this_page.replace(u"\xa0", " ").strip().split())
    pages.append(this_page.encode("ascii", "xmlcharrefreplace"))
for page in pages:
    print '*' * 80
    print page

但是这个脚本忽略了换行符,给我留下了一些乱七八糟的字符串,比如 information contained an individual which, because of name, identifyingnumber, mark or description (即,这应该读作 identifying number,而不是 identifyingumber)。

Here's an example我尝试解析的 PDF 类型。

最佳答案

我不太了解 PDF 编码,但我认为您可以通过修改 pdf.py 来解决您的特定问题。在 PageObject.extractText 方法中,您会看到发生了什么:

def extractText(self):
    [...]
    for operands,operator in content.operations:
        if operator == "Tj":
            _text = operands[0]
            if isinstance(_text, TextStringObject):
                text += _text
        elif operator == "T*":
            text += "\n"
        elif operator == "'":
            text += "\n"
            _text = operands[0]
            if isinstance(_text, TextStringObject):
                text += operands[0]
        elif operator == '"':
            _text = operands[2]
            if isinstance(_text, TextStringObject):
                text += "\n"
                text += _text
        elif operator == "TJ":
            for i in operands[0]:
                if isinstance(i, TextStringObject):
                    text += i

如果运算符是 TjTJ(在您的示例 PDF 中是 Tj),则仅附加文本,不添加换行符。现在您不一定想要添加换行符,至少如果我正在正确阅读 PDF 引用:Tj/TJ 只是单个和多个显示字符串运算符,并且某种分隔符的存在不是强制性的。

无论如何,如果您将此代码修改为类似

def extractText(self, Tj_sep="", TJ_sep=""):

[...]

        if operator == "Tj":
            _text = operands[0]
            if isinstance(_text, TextStringObject):
                text += Tj_sep
                text += _text

[...]

        elif operator == "TJ":
            for i in operands[0]:
                if isinstance(i, TextStringObject):
                    text += TJ_sep
                    text += i

那么默认行为应该是相同的:

In [1]: pdf.getPage(1).extractText()[1120:1250]
Out[1]: u'ing an individual which, because of name, identifyingnumber, mark or description can be readily associated with a particular indiv'

但您可以在需要时更改它:

In [2]: pdf.getPage(1).extractText(Tj_sep=" ")[1120:1250]
Out[2]: u'ta" means any information concerning an individual which, because of name, identifying number, mark or description can be readily '

In [3]: pdf.getPage(1).extractText(Tj_sep="\n")[1120:1250]
Out[3]: u'ta" means any information concerning an individual which, because of name, identifying\nnumber, mark or description can be readily '

或者,您可以通过就地修改操作数本身来简单地自己添加分隔符,但这可能会破坏其他东西(像 get_original_bytes 这样的方法让我很紧张)。

最后,如果您不想编辑 pdf.py 本身:您可以简单地将此方法提取到一个函数中。

关于python - pyPdf 忽略 PDF 文件中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017379/

相关文章:

java - 计算两个字符串有多少个重复字符

java - 如何将字符串转换为 Java 字符串文字?

java - 有没有一种方法可以切换字符串中的字母?

linux - 自动连接 PDF 中的文本框

java - 自动 PDF 验证

python - 如何删除 python 字典中的键/值对?

python - 如何处理 `pickle.load`调用尚未准备好使用的 `__setitem__`?

python - python中如何选择pocketSphinx的语言模型

php - 在 PHP 中上传、查看 PDF

python - 如何克隆 venv 虚拟环境?