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)