Gia*_*rlo 10 python pdf stream pypdf
按照这个例子,我可以将所有元素列入pdf文件
import pyPdf
pdf = pyPdf.PdfFileReader(open("pdffile.pdf"))
list(pdf.pages) # Process all the objects.
print pdf.resolvedObjects
Run Code Online (Sandbox Code Playgroud)
现在,我需要从pdf文件中提取非标准对象.
我的对象是名为MYOBJECT的对象,它是一个字符串.
由关注我的python脚本打印的作品是:
{'/MYOBJECT': IndirectObject(584, 0)}
Run Code Online (Sandbox Code Playgroud)
pdf文件是这样的:
558 0 obj
<</Contents 583 0 R/CropBox[0 0 595.22 842]/MediaBox[0 0 595.22 842]/Parent 29 0 R/Resources
<</ColorSpace <</CS0 563 0 R>>
/ExtGState <</GS0 568 0 R>>
/Font<</TT0 559 0 R/TT1 560 0 R/TT2 561 0 R/TT3 562 0 R>>
/ProcSet[/PDF/Text/ImageC]
/Properties<</MC0<</MYOBJECT 584 0 R>>/MC1<</SubKey 582 0 R>> >>
/XObject<</Im0 578 0 R>>>>
/Rotate 0/StructParents 0/Type/Page>>
endobj
...
...
...
584 0 obj
<</Length 8>>stream
1_22_4_1 --->>>> this is the string I need to extract from the object
endstream
endobj
Run Code Online (Sandbox Code Playgroud)
如何跟踪该584值以引用我的字符串(当然在pyPdf下)?
每个元素pdf.pages都是一个字典,所以假设它在第1页,pdf.pages[0]['/MYOBJECT']应该是你想要的元素.
你可以尝试打印单独或在它戳help,并dir在Python提示更多关于如何得到你想要的字符串
编辑:
收到pdf的副本后,我发现对象在,pdf.resolvedObjects[0][558]['/Resources']['/Properties']['/MC0']['/MYOBJECT']并且可以通过getData()检索值
下面的函数提供了一种更通用的方法来通过递归查找有问题的密钥来解决这个问题
import types
import pyPdf
pdf = pyPdf.PdfFileReader(open('file.pdf'))
pages = list(pdf.pages)
def findInDict(needle,haystack):
for key in haystack.keys():
try:
value = haystack[key]
except:
continue
if key == needle:
return value
if type(value) == types.DictType or isinstance(value,pyPdf.generic.DictionaryObject):
x = findInDict(needle,value)
if x is not None:
return x
answer = findInDict('/MYOBJECT',pdf.resolvedObjects).getData()
Run Code Online (Sandbox Code Playgroud)
IndirectObject 指的是一个实际的对象(它就像一个链接或别名,这样当相同的内容出现在多个地方时可以减小 PDF 的总大小)。getObject 方法将为您提供实际的对象。
如果对象是文本对象,那么只需在对象上执行 str() 或 unicode() 即可获得其中的数据。
或者,pyPdf 将对象存储在 resolveObjects 属性中。例如,包含此对象的 PDF:
13 0 obj
<< /Type /Catalog /Pages 3 0 R >>
endobj
Run Code Online (Sandbox Code Playgroud)
可以这样读:
>>> import pyPdf
>>> pdf = pyPdf.PdfFileReader(open("pdffile.pdf"))
>>> pages = list(pdf.pages)
>>> pdf.resolvedObjects
{0: {2: {'/Parent': IndirectObject(3, 0), '/Contents': IndirectObject(4, 0), '/Type': '/Page', '/Resources': IndirectObject(6, 0), '/MediaBox': [0, 0, 595.2756, 841.8898]}, 3: {'/Kids': [IndirectObject(2, 0)], '/Count': 1, '/Type': '/Pages', '/MediaBox': [0, 0, 595.2756, 841.8898]}, 4: {'/Filter': '/FlateDecode'}, 5: 147, 6: {'/ColorSpace': {'/Cs1': IndirectObject(7, 0)}, '/ExtGState': {'/Gs2': IndirectObject(9, 0), '/Gs1': IndirectObject(10, 0)}, '/ProcSet': ['/PDF', '/Text'], '/Font': {'/F1.0': IndirectObject(8, 0)}}, 13: {'/Type': '/Catalog', '/Pages': IndirectObject(3, 0)}}}
>>> pdf.resolvedObjects[0][13]
{'/Type': '/Catalog', '/Pages': IndirectObject(3, 0)}
Run Code Online (Sandbox Code Playgroud)