Browse Source

pdf_watermark.py

mit Manuel programmiert
Global Cube 2 years ago
parent
commit
50074428e6

+ 20 - 0
sandbox/hello.py

@@ -0,0 +1,20 @@
+def main():
+    name = input("Wie heißt du? ")
+
+    if name == "Sabina":
+        print("Falsch!")
+        return False
+    else:
+        print("Hallo " + name + "!")
+        return True
+
+
+result = True
+
+# while result:
+#     result = main()
+
+names = ['Robert', 'Toni', 'Sabina', 'Manuel']
+
+for name in sorted(reversed(names)):
+    print("Hallo " + name)

BIN
tools/pdf/data/Rechnung Miete_Ulmen2.pdf


BIN
tools/pdf/data/Sammelrechnung Monatsmiete Keyloop.pdf


+ 1 - 1
tools/pdf/data/example-gc.pdf → tools/pdf/data/mit_Briefpapier/Rechnung Miete_Ulmen.pdf

@@ -37,7 +37,7 @@ endobj
 /Properties <<
 /MC0 6 0 R
 >>
-/ProcSet [ /ImageC /Text /PDF ]
+/ProcSet [ /PDF /ImageC /Text ]
 >>
 /Contents 5 0 R
 /Annots [ ]

File diff suppressed because it is too large
+ 943 - 0
tools/pdf/data/mit_Briefpapier/Rechnung Miete_Ulmen2.pdf


File diff suppressed because it is too large
+ 1909 - 0
tools/pdf/data/mit_Briefpapier/Sammelrechnung Monatsmiete Keyloop.pdf


+ 39 - 23
tools/pdf/pdf_watermark.py

@@ -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/')

+ 0 - 0
tools/pdf/data/Briefbogen_GC_Jahnstr_GmbH.pdf → tools/pdf/template/Briefbogen_GC_Jahnstr_GmbH.pdf


Some files were not shown because too many files changed in this diff