我正在尝试使用PDFrw阅读示例PDF。 PDF在左下角坐标Hello Matthew处包含短语(100, 100)。当我尝试输出文本时(如果可以的话),我得到了数据流。我似乎无法弄清楚如何将其作为文本。

>>> import pdfrw

>>> file_object = pdfrw.PdfReader("Hello.pdf")
>>> file_object
{'/ID': ['<f643bc0910dfb67725d53e11054f4609>', '<f643bc0910dfb67725d53e11054f4609>'], '/Info': (5, 0), '/Root': {'/Outl
ines': (8, 0), '/PageMode': '/UseNone', '/Pages': {'/Count': '1', '/Kids': [{'/Contents': (7, 0), '/MediaBox': ['0', '0
', '595.2756', '841.8898'], '/Parent': {...}, '/Resources': {'/Font': (1, 0), '/ProcSet': ['/PDF', '/Text', '/ImageB',
'/ImageC', '/ImageI']}, '/Rotate': '0', '/Trans': {}, '/Type': '/Page'}], '/Type': '/Pages'}, '/Type': '/Catalog'}, '/S
ize': '9'}

>>> file_object.pages[0]
{'/Contents': (7, 0), '/MediaBox': ['0', '0', '595.2756', '841.8898'], '/Parent': {'/Count': '1', '/Kids': [{...}], '/T
ype': '/Pages'}, '/Resources': {'/Font': (1, 0), '/ProcSet': ['/PDF', '/Text', '/ImageB', '/ImageC', '/ImageI']}, '/Rot
ate': '0', '/Trans': {}, '/Type': '/Page'}

>>> file_object.pages[0].keys()
['/Contents', '/MediaBox', '/Parent', '/Resources', '/Rotate', '/Trans', '/Type']

>>> file_object.pages[0].Contents
{'/Filter': ['/ASCII85Decode', '/FlateDecode'], '/Length': '102'}

>>> file_object.pages[0].Contents.stream
'GapQh0E=F,0U\\H3T\\pNYT^QKk?tc>IP,;W#U1^23ihPEM_?CW4KISi90EC-p>QkRte=<%V"lI7]P)Rn29neZ[Kb,htEWn&q7Q2"V~>'

最佳答案

该流被压缩。您可以通过字典/ Filter参数来判断。

不幸的是,pdfrw还不知道如何使用这种类型的过滤器解压缩。如果先通过pdftk之类的文件运行pdf文件以进行解压缩,则可能会看到更合理的文件。

免责声明:我是pdfrw的主要作者。

但...

即使这样,特别是对于非ASCII字体,PDF中的字符到字形的映射也很复杂,因此您将不会总是看到看起来合理的东西。

如果您真的想深入研究文本PDF文件,则pdfminer可能会更有用-pdfrw尚未真正开发出能够很好地完成此工作的工具。

08-27 06:15