views:

120

answers:

3

Hi guys,

I am not too sure the best way to word this, but what I want to do, is read a pdf file, make various modifications, and save the modified pdf over the original file. As of now, I am able to save the modified pdf to a separate file, but I am looking to replace the original, not create a new file.

Here is my current code:

from pyPdf import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input = PdfFileReader(file('input.pdf', 'rb'))
blank = PdfFileReader(file('C:\\BLANK.pdf', 'rb'))

# Copy the input pdf to the output.
for page in range(int(input.getNumPages())):
    output.addPage(input.getPage(page))

# Add a blank page if needed.
if (input.getNumPages() % 2 != 0):
    output.addPage(blank.getPage(0))

# Write the output to pdf.
outputStream = file('input.pdf', 'wb')
output.write(outputStream)
outputStream.close()

If i change the outputStream to a different file name, it works fine, I just cant save over the input file because it is still being used. I have tried to .close() the stream, but it was giving me errors as well.

I have a feeling this has a fairly simple solution, I just haven't had any luck finding it.

Thanks!

+7  A: 

You can always rename the temporary output file to the old file:

import os
f = open('input.pdf', 'rb')
# do stuff to temp.pdf
f.close()
os.rename('temp.pdf', 'input.pdf')
Steve
+2  A: 

You said you tried to close() the stream but got errors? You could delete the PdfFileReader objects to ensure nobody still has access to the stream. And then close the stream.

from pyPdf import PdfFileWriter, PdfFileReader

inputStream = file('input.pdf', 'rb')
blankStream = file('C:\\BLANK.pdf', 'rb')
output = PdfFileWriter()
input = PdfFileReader(inputStream)
blank = PdfFileReader(blankStream)

...

del input # PdfFileReader won't mess with the stream anymore
inputStream.close()
del blank
blankStream.close()

# Write the output to pdf.
outputStream = file('input.pdf', 'wb')
output.write(outputStream)
outputStream.close()
Robert
+1  A: 

If the PDFs are small enough (that'll depend on your platform), you could just read the whole thing in, close the file, modify the data, then write the whole thing back over the same file.

dash-tom-bang