用于间接对象提取的 pyPdf
按照这个示例,我现在可以将所有元素列出到 pdf 文件中
import pyPdf
pdf = pyPdf.PdfFileReader(open("pdffile.pdf"))
list(pdf.pages) # Process all the objects.
print pdf.resolvedObjects
,我需要从 pdf 文件中提取非标准对象。
我的对象是名为 MYOBJECT 的对象,它是一个字符串。
与我有关的 python 脚本打印的部分是:
{'/MYOBJECT': IndirectObject(584, 0)}
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
如何遵循 584 值来引用我的字符串(当然在 pyPdf 下)?
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 the objects.
print pdf.resolvedObjects
now, I need to extract a non-standard object from the pdf file.
My object is the one named MYOBJECT and it is a string.
The piece printed by the python script that concernes me is:
{'/MYOBJECT': IndirectObject(584, 0)}
The pdf file is this:
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
How can I follow the 584
value in order to refer to my string (under pyPdf of course)??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pdf.pages
中的每个元素都是一个字典,因此假设它位于第 1 页,pdf.pages[0]['/MYOBJECT']
应该是您想要的元素。您可以尝试单独打印它,或者在 python 提示符下使用
help
和dir
来查看它,以了解有关如何获取所需字符串的更多信息编辑:
收到副本后pdf,我在
pdf.resolvedObjects[0][558]['/Resources']['/Properties']['/MC0']['/MYOBJECT']
找到了该对象,并且可以通过 getData() 检索值,以下函数提供了一种更通用的方法,通过递归查找有问题的键来解决此问题
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
anddir
in a python prompt for more about how to get the string you wantEdit:
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
IndirectObject 指的是实际对象(它类似于链接或别名,以便当相同内容出现在多个位置时可以减小 PDF 的总大小)。 getObject 方法将为您提供实际的对象。
如果对象是文本对象,那么只需在对象上执行 str() 或 unicode() 即可获取其中的数据。
或者,pyPdf 将对象存储在resolvedObjects 属性中。 例如,包含此对象的 PDF:
可以使用以下命令读取:
An IndirectObject refers to an actual object (it's like a link or alias so that the total size of the PDF can be reduced when the same content appears in multiple places). The getObject method will give you the actual object.
If the object is a text object, then just doing a str() or unicode() on the object should get you the data inside of it.
Alternatively, pyPdf stores the objects in the resolvedObjects attribute. For example, a PDF that contains this object:
Can be read with this:
如果到处寻找物体,耶希亚的方法就很好。 我的猜测(查看 PDF)是它总是在同一个位置(第一页,在“MC0”属性中),因此查找字符串的更简单的方法是:
Jehiah's method is good if looking everywhere for the object. My guess (looking at the PDF) is that it is always in the same place (the first page, in the 'MC0' property), and so a much simpler method of finding the string would be: