我正在尝试使用pyPdf从大型pdf中提取几页到单独的文件。每当我这样做时,生成的文件大小几乎与源文件相同。我认为它与文件中的书签有关,因为如果页面不包含任何链接,输出文件的大小非常小。我无法弄清楚如何从输出文件中排除书签。
from pyPdf import PdfFileWriter as writer, PdfFileReader as reader
w = writer()
r = reader(open('9.pdf'))
for p in xrange(5):
w.addPage(r.getPage(p))
with open('out.pdf', 'wb') as stream:
w.write(stream)
w._objects
# prints:
{'/Kids': [IndirectObject(4, 0), IndirectObject(5, 0), IndirectObject(6, 0), IndirectObject(7, 0), IndirectObject(8, 0)], '/Type': '/Pages', '/Count': 5}
{'/Producer': u'Python PDF Library - http://pybrary.net/pyPdf/'}
{'/Type': '/Catalog', '/Pages': IndirectObject(1, 0)}
{'/Parent': IndirectObject(1, 0), '/Rotate': 0, '/Contents': IndirectObject(4307, 0), '/Resources': {'/ColorSpace': {'/CS1': IndirectObject(4309, 0), '/CS0': IndirectObject(4305, 0)}, '/XObject': {'/Im0': IndirectObject(4312, 0)}, '/ExtGState': {'/GS2': IndirectObject(4324, 0), '/GS1': IndirectObject(4323, 0), '/GS0': IndirectObject(4306, 0)}, '/Font': {'/T1_2': IndirectObject(4308, 0), '/T1_0': IndirectObject(4303, 0), '/T1_1': IndirectObject(4304, 0)}, '/ProcSet': ['/PDF', '/Text', '/ImageB']}, '/CropBox': [0, 0, 612, 792], '/BCLPrivAnnots': {'/BCLC_BCL_Jade': []}, '/MediaBox': [0, 0, 612, 792], '/Annots': IndirectObject(4301, 0), '/Type': '/Page'}
{'/Parent': IndirectObject(1, 0), '/Contents': IndirectObject(2, 0), '/Resources': {'/ColorSpace': {'/CS1': IndirectObject(4309, 0), '/CS0': IndirectObject(4305, 0)}, '/ExtGState': {'/GS2': IndirectObject(3417, 0), '/GS1': IndirectObject(3412, 0), '/GS0': IndirectObject(4306, 0)}, '/Font': {'/T1_2': IndirectObject(3413, 0), '/T1_0': IndirectObject(3415, 0), '/T1_1': IndirectObject(3416, 0)}, '/ProcSet': ['/PDF', '/Text']}, '/Rotate': 0, '/CropBox': [0, 0, 612, 792], '/BCLPrivAnnots': {'/BCLC_BCL_Jade': []}, '/MediaBox': [0, 0, 612, 792], '/Thumb': IndirectObject(3920, 0), '/Type': '/Page'}
{'/Parent': IndirectObject(1, 0), '/Contents': IndirectObject(4, 0), '/Resources': {'/ColorSpace': {'/CS0': IndirectObject(4305, 0)}, '/ExtGState': {'/GS0': IndirectObject(4306, 0)}, '/Font': {'/T1_2': IndirectObject(3425, 0), '/T1_3': IndirectObject(3428, 0), '/T1_0': IndirectObject(3426, 0), '/T1_1': IndirectObject(3427, 0)}, '/ProcSet': ['/PDF', '/Text']}, '/Rotate': 0, '/CropBox': [0, 0, 612, 792], '/BCLPrivAnnots': {'/BCLC_BCL_Jade': []}, '/MediaBox': [0, 0, 612, 792], '/Thumb': IndirectObject(3921, 0), '/Type': '/Page'}
{'/Parent': IndirectObject(1, 0), '/Contents': IndirectObject(6, 0), '/Resources': {}, '/Rotate': 0, '/CropBox': [0, 0, 612, 792], '/BCLPrivAnnots': {'/BCLC_BCL_Jade': []}, '/MediaBox': [0, 0, 612, 792], '/Thumb': IndirectObject(3922, 0), '/Type': '/Page'}
{'/Parent': IndirectObject(1, 0), '/Contents': IndirectObject(9, 0), '/Resources': IndirectObject(8, 0), '/Rotate': 0, '/CropBox': [0, 0, 612, 792], '/BCLPrivAnnots': {'/BCLC_BCL_Jade': []}, '/MediaBox': [0, 0, 612, 792], '/Thumb': IndirectObject(3923, 0), '/Type': '/Page'}
答案 0 :(得分:-1)
使用PyPdf时,输出文件几乎是所有格式的条带化。
具体而言,替换:
with open('out.pdf', 'wb') as stream:
w.write(stream)
有:
stream = file('out.pdf', 'wb')
w.write(stream)
stream.close()
然后查看最终结果。
写作:
也是一种好习惯fin = open('9.pdf')
r = reader(fin)
fin.close()
而不是:
r = reader(open('9.pdf'))