【发布时间】:2013-12-18 02:59:41
【问题描述】:
有没有办法从 pdf 文档中提取图像作为流(使用PyPDF2 库)? 是否可以将一些图像替换为另一个(例如使用 PIL 生成或从文件加载)?
我能够从 pdf 对象树中获取 EncodedStreamObject 并获取编码流(通过调用 getData() 方法),但看起来它只是没有任何图像标题和其他元信息的原始内容。
>>> import PyPDF2
>>> # sample.pdf contains png images
>>> reader = PyPDF2.PdfFileReader(open('sample.pdf', 'rb'))
>>> reader.resolvedObjects[0][9]
{'/BitsPerComponent': 8,
'/ColorSpace': ['/ICCBased', IndirectObject(20, 0)],
'/Filter': '/FlateDecode',
'/Height': 30,
'/Subtype': '/Image',
'/Type': '/XObject',
'/Width': 100}
>>>
>>> reader.resolvedObjects[0][9].__class__
PyPDF2.generic.EncodedStreamObject
>>>
>>> s = reader.resolvedObjects[0][9].getData()
>>> len(s), s[:10]
(9000, '\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc')
我已经看过很多 PyPDF2、ReportLab 和 PDFMiner 解决方案,但没有找到像我正在寻找的东西。
任何代码示例和链接都会很有帮助。
【问题讨论】:
-
所以您想打开一个大的 pdf,提取一个页面,然后将该页面添加到现有的 pdf 中?可以将合并后的 pdf 保存为新文件吗?
-
这个答案可能会有所帮助:stackoverflow.com/a/34116472/1513933
标签: python pdf image-processing reportlab pypdf