2012-02-15 40 views
1

我試圖使用pyPdf從大pdf中提取幾頁到單獨的文件。每當我這樣做,結果文件大小几乎與源文件相同。我認爲它與文件內的書籤有關,因爲如果頁面不包含任何鏈接,則輸出文件的大小非常小。我無法弄清楚如何從輸出文件中排除書籤。pyPdf輸出文件是相同的大小,無論頁數

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'} 
+1

不要忘記,pyPdf不壓縮輸出中,當你使用的輸入可能是壓縮。 – 2012-04-05 10:41:14

回答

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

如何比使用'with'更好,即使拋出異常時也總是關閉文件? – 2012-04-05 08:10:50

+0

我有一個腳本使用'with'破壞了一個pdf文件。最好安全一點。 – user850498 2012-04-05 08:38:34

+3

但是'帶'是安全的一面。 – 2012-04-05 08:54:28

相關問題