Skip to main content
 首页 » 编程设计

python之在 Python 的 PDFrw 中使用流

2024年10月01日8lvdongjie

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

>>> 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 还没有真正开发出可以很好地完成这项工作的工具。