Python pyPdf输出文件大小相同,与页面计数无关

Python pyPdf输出文件大小相同,与页面计数无关,python,pdf,bookmarks,pypdf,Python,Pdf,Bookmarks,Pypdf,我正在尝试使用pyPdf将一些页面从一个大的pdf文件提取到一个单独的文件中。无论何时,生成的文件大小几乎与源文件相同。我认为这与文件中的书签有关,因为如果页面不包含任何链接,则输出文件的大小非常小。我不知道如何从输出文件中排除书签 from pyPdf import PdfFileWriter as writer, PdfFileReader as reader w = writer() r = reader(open('9.pdf')) for p in xrange(5): w.

我正在尝试使用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'}

当使用PyPdf时,输出文件几乎所有格式都被分条

具体而言,替换:

with open('out.pdf', 'wb') as stream:
    w.write(stream)
用于:

然后看看最终结果

写作也是一种很好的做法:

fin = open('9.pdf')
r = reader(fin)
fin.close()
而不是:

r = reader(open('9.pdf'))

这比使用“with”更好吗?它总是关闭文件,即使抛出异常也是如此。我有一个脚本使用
with
破坏了pdf文件。最好站在安全的一边。但是,
with
是安全的一边。。。。我不明白这是怎么回事。。我想我会坚持使用“with”别忘了pyPdf不会压缩输出,而您使用的输入可能是压缩的。
r = reader(open('9.pdf'))