0
votes

i was making a script that watermarks all pdfs in a folder. It worked but then i was tweaking it so the watermarked pdfs moved into a destination folder and all of a sudden i can't get it to work anymore...

Error:

Traceback (most recent call last): File "pdf_watermarker_v2.py", line 25, in source_read = PyPDF2.PdfFileReader(source_open) File "C:\Users\niels\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__ self.read(stream) File "C:\Users\niels\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1689, in read stream.seek(-1, 2) OSError: [Errno 22] Invalid argument

code :

import PyPDF2
import os
import sys


#This wil watermark all files in the folder with a watermarker
watermark = sys.argv[1]
destination = sys.argv[2]
source = os.listdir()

#def watermark_all(watermark,source,destination):

if not os.path.exists(destination):
    os.mkdir(destination)


with open(watermark,'rb') as watermark_open:
    watermark_read = PyPDF2.PdfFileReader(watermark_open)

watermark_pdf = watermark_read.getPage(0)

for i in source:
    if i.endswith('.pdf'):
        with open(i,'rb') as source_open:
            source_read = PyPDF2.PdfFileReader(source_open)

            source_num = source_read.getNumPages()

            source_real = int(source_num) -1

            source_pdf = source_read.getPage(source_real)

            source_pdf.mergePage(watermark_pdf)

            writer = PyPDF2.PdfFileWriter()
            writer.addPage(source_pdf)
            with open(i,'wb') as merger_file:
                writer.write(merger_file)
1
Aside from the usual advice to provide a minimal reproducible example, I'd also suggest posting the code that worked and the changes that you made that caused it to stop working.jjramsey
This code worked.. thats the issue.BLOEDLINK
So has the code that you posted been changed at all? Is it a change in some code that you haven't shown us that caused it to stop working?jjramsey

1 Answers

0
votes

Ok so i found the answer. For some reason, if i try watermarking these files again, it gives this error. Even though all files are still in the folder and the merge can still happen file wise, for some reason it won't merge again with the same files when running the script again. I had to replace the files with untouched files and then i could run the script again.