pyPdf for IndirectObject extraction

前端 未结 3 2189
不思量自难忘°
不思量自难忘° 2020-12-08 23:07

Following this example, I can list all elements into a pdf file

import pyPdf
pdf = pyPdf.PdfFileReader(open(\"pdffile.pdf\"))
list(pdf.pages) # Process all t         


        
3条回答
  •  没有蜡笔的小新
    2020-12-08 23:46

    each element in pdf.pages is a dictionary, so assuming it's on page 1, pdf.pages[0]['/MYOBJECT'] should be the element you want.

    You can try to print that individually or poke at it with help and dir in a python prompt for more about how to get the string you want

    Edit:

    after receiving a copy of the pdf, i found the object at pdf.resolvedObjects[0][558]['/Resources']['/Properties']['/MC0']['/MYOBJECT'] and the value can be retrieved via getData()

    the following function gives a more generic way to solve this by recursively looking for the key in question

    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()
    

提交回复
热议问题