pyPdf输出文件大小相同,无论页数如何 [英] pyPdf output files are the same size regardless of page count

查看:77
本文介绍了pyPdf输出文件大小相同,无论页数如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pyPdf将大pdf的几页内容提取到一个单独的文件中.每当我这样做时,生成的文件大小几乎与源文件相同.我认为这与文件内的书签有关,因为如果页面不包含任何链接,输出文件的大小将非常小.我不知道如何从输出文件中排除书签.

I'm trying to use pyPdf to extract a few pages from a large pdf to a separate file. Whenever I do, the resulting filesize is nearly identical to the source file. I think it has something to do with the bookmarks inside the files, because it the output file size is very small if the page doesn't contain any links. I can't figure out how to exclude the bookmarks from the output file.

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时,输出文件几乎是所有格式的条带.

When using PyPdf, the output file is striped of almost all formatting.

具体来说,替换为:

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

这篇关于pyPdf输出文件大小相同,无论页数如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆