import PyPDF2 import plac from pathlib import Path from doc2pdf import doc2pdf def add_watermark(input_file): watermark_file = 'c:/tools/template/Briefbogen_GC_Jahnstr_GmbH.pdf' output_file = Path(input_file).parent.joinpath("mit_Briefpapier", Path(input_file).name) if not output_file.parent.exists(): output_file.parent.mkdir() with open(input_file, "rb") as filehandle_input: # read content of the original file pdf = PyPDF2.PdfFileReader(filehandle_input) with open(watermark_file, "rb") as filehandle_watermark: # read content of the watermark watermark = PyPDF2.PdfFileReader(filehandle_watermark) # get first page of the watermark PDF first_page_watermark = watermark.getPage(0) # create a pdf writer object for the output file pdf_writer = PyPDF2.PdfFileWriter() # get first page of the original PDF for i in range(pdf.getNumPages()): current_page = pdf.getPage(i) # merge the two pages current_page.mergePage(first_page_watermark) # add page pdf_writer.addPage(current_page) with open(output_file, "wb") as filehandle_output: # write the watermarked file to the new file pdf_writer.write(filehandle_output) def add_watermark_to_folder(base_dir): for filename in Path(base_dir).glob('*.doc'): doc2pdf(str(filename)) for filename in Path(base_dir).glob('*.pdf'): add_watermark(str(filename)) print(filename.name) if __name__ == '__main__': plac.call(add_watermark_to_folder) # add_watermark_to_folder(str(Path(__file__).parent) + '/data/')