I'm not too sure that this is the best way to talk about this, but what I want to do is read the pdf file, make various modifications and save the modified pdf file compared to the original file. At the moment, I can save the modified pdf file to a separate file, but I'm looking to replace the original, and 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'))
for page in range(int(input.getNumPages())):
output.addPage(input.getPage(page))
if (input.getNumPages() % 2 != 0):
output.addPage(blank.getPage(0))
outputStream = file('input.pdf', 'wb')
output.write(outputStream)
outputStream.close()
If I change outputStreamto a different file name, it works fine, I just can't save the input file because it is still in use. I tried to .close()stream, but it also gave me errors.
I have a feeling that this one has a pretty simple solution, I just had no luck finding it.
Thank!