|
@@ -1,35 +1,51 @@
|
|
|
import PyPDF2
|
|
|
import os
|
|
|
+import plac
|
|
|
+from pathlib import Path
|
|
|
|
|
|
|
|
|
-base_dir = os.path.dirname(__file__)
|
|
|
-input_file = base_dir + "/data/Rechnung Miete_Ulmen.pdf"
|
|
|
-output_file = base_dir + "/data/example-gc.pdf"
|
|
|
-watermark_file = base_dir + "/data/Briefbogen_GC_Jahnstr_GmbH.pdf"
|
|
|
+watermark_file = os.path.dirname(__file__) + '/template/Briefbogen_GC_Jahnstr_GmbH.pdf'
|
|
|
|
|
|
-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)
|
|
|
+def add_watermark(input_file):
|
|
|
+ output_file = Path(input_file).parent.joinpath("mit_Briefpapier", Path(input_file).name)
|
|
|
+ if not output_file.parent.exists():
|
|
|
+ output_file.parent.mkdir()
|
|
|
|
|
|
- # get first page of the original PDF
|
|
|
- first_page = pdf.getPage(0)
|
|
|
+ with open(input_file, "rb") as filehandle_input:
|
|
|
+ # read content of the original file
|
|
|
+ pdf = PyPDF2.PdfFileReader(filehandle_input)
|
|
|
|
|
|
- # get first page of the watermark PDF
|
|
|
- first_page_watermark = watermark.getPage(0)
|
|
|
+ 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)
|
|
|
|
|
|
- # merge the two pages
|
|
|
- first_page.mergePage(first_page_watermark)
|
|
|
+ # create a pdf writer object for the output file
|
|
|
+ pdf_writer = PyPDF2.PdfFileWriter()
|
|
|
|
|
|
- # 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)
|
|
|
|
|
|
- # add page
|
|
|
- pdf_writer.addPage(first_page)
|
|
|
+ # merge the two pages
|
|
|
+ current_page.mergePage(first_page_watermark)
|
|
|
|
|
|
- with open(output_file, "wb") as filehandle_output:
|
|
|
- # write the watermarked file to the new file
|
|
|
- pdf_writer.write(filehandle_output)
|
|
|
+ # 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('*.pdf'):
|
|
|
+ add_watermark(str(filename))
|
|
|
+ print(filename.name)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ plac.call(add_watermark_to_folder)
|
|
|
+ # add_watermark_to_folder(os.path.dirname(__file__) + '/data/')
|