pyPdf忽略PDF文件中的换行符 [英] pyPdf ignores newlines in PDF file

查看:108
本文介绍了pyPdf忽略PDF文件中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to extract each page of a PDF as a string:

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 concerning an individual which, because of name, identifyingnumber, mark or description这样的凌乱字符串(即,它应显示为identifying number,而不是identifyingumber).

But this script ignore newline characters, leaving me with messy strings like information concerning an individual which, because of name, identifyingnumber, mark or description (i.e, this should read identifying number, not identifyingumber).

这是一个PDF类型的示例我正在尝试解析.

推荐答案

我对PDF编码了解不多,但是我认为您可以通过修改pdf.py来解决您的特定问题.在PageObject.extractText方法中,您看到正在发生的事情:

I don't know much about PDF encoding, but I think you can solve your particular problem by modifying pdf.py. In the PageObject.extractText method, you see what's going on:

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仅仅是单个和多个show-string运算符,并且存在一个某种分隔符不是必需的.

If the operator is Tj or TJ (it's Tj in your example PDF) then the text is simply appended and no newline is added. Now you wouldn't necessarily want to add a newline, at least if I'm reading the PDF reference right: Tj/TJ are simply the single and multiple show-string operators, and the existence of a separator of some kind isn't mandatory.

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

Anyway, if you modify this code to be something like

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'

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

but you can change it when you want to:

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之类的方法使我感到紧张).

Alternatively, you could simply add the separators yourself by modifying the operands themselves in-place, but that could break something else (methods like get_original_bytes make me nervous).

最后,如果您不想这样做,则不必编辑pdf.py本身:您可以简单地将此方法提取到函数中.

Finally, you don't have to edit pdf.py itself if you don't want to: you could simply pull out this method into a function.

这篇关于pyPdf忽略PDF文件中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆