Browse Source

Versand.csv in eine Datei zusammenführen

gc-server3 2 months ago
parent
commit
c58e7004a6
3 changed files with 125 additions and 2 deletions
  1. 1 0
      gctools.bat
  2. 106 0
      misc/convert_mail_csv.py
  3. 18 2
      misc2.py

+ 1 - 0
gctools.bat

@@ -1,4 +1,5 @@
 cd /d %~dp0
 call venv\Scripts\activate.bat
 pyinstaller -F --path %~dp0 --icon favicon.ico gctools.py
+copy /Y %~dp0\dist\gctools.exe P:\GCTools
 pause

+ 106 - 0
misc/convert_mail_csv.py

@@ -0,0 +1,106 @@
+import csv
+from pathlib import Path
+
+from misc.file_backup import copy_file_with_timestamp
+
+
+def count_unique_emails(csv_file):
+    emails = set()
+    with open(csv_file, "r") as file:
+        reader = csv.reader(file, delimiter=";")
+        for row in reader:
+            if row[2] in ["Empfaenger", ""]:
+                continue
+            emails.add(row[2])
+    with open(str(Path(csv_file).parent) + "\\email_empfaenger.csv", "w") as fwh:
+        for email in sorted(emails):
+            fwh.write(email + "\n")
+    return len(emails)
+
+
+def convert_csv_in_folder(folder, export_csv, all_reports=False):
+    row_count = 0
+
+    with open(export_csv, "w") as fwh:
+        fwh.write("Versand;Datei;Empfaenger\n")
+        for file in Path(folder).glob("*.csv"):
+            with file.open("r") as frh:
+                reader = csv.reader(frh, delimiter=";")
+                for row in reader:
+                    if row[1] in ["Empfaenger"]:
+                        continue
+                    if row[1] == "":
+                        if all_reports:
+                            fwh.write(file.name + ";" + row[0] + ";\n")
+                            row_count += 1
+                        continue
+
+                    email_list = row[1].lower().split(",")
+                    for email in sorted(email_list):
+                        fwh.write(file.name + ";" + row[0] + ";" + email.strip() + "\n")
+                        row_count += 1
+    return row_count
+
+
+def convert_combined_to_folder(csv_file, export_folder):
+    result = {}
+    with open(csv_file, "r") as frh:
+        reader = csv.reader(frh, delimiter=";")
+
+        for row in reader:
+            if row[0] in ["Versand"]:
+                # Kopfzeile ignorieren
+                continue
+            if row[0] not in result:
+                result[row[0]] = {}
+            if row[1] not in result[row[0]]:
+                result[row[0]][row[1]] = []
+            result[row[0]][row[1]].append(row[2])
+
+            # if row[0] != current_file:
+            #     if fwh is not None:
+            #         fwh.close()
+            #     current_file = row[0]
+            #     fwh = open(f"{export_folder}\\{current_file}", "w")
+
+            # fwh.write(row[1] + ";" + row[2] + "\n")
+            # fwh.close()
+    # json.dump(result, open("export2\\test.json", "w"), indent=2)
+    for file, reports in result.items():
+        print(file)
+        with open(f"{export_folder}\\{file}", "w") as fwh:
+            fwh.write("Datei;Empfaenger\n")
+            for report, mails in reports.items():
+                fwh.write(report + ";" + ",".join(sorted(mails)) + "\n")
+
+
+def mail_csv_backup(base_dir: str) -> None:
+    for f in Path(base_dir).glob("*.csv"):
+        # print("* " + f.name)
+        copy_file_with_timestamp(str(f), str(f.parent) + "\\backup")
+
+
+def convert_mail_csv(csv_file):
+    pass
+
+
+def convert_folder_to_combined_csv(path: str):
+    print("Sicherung der Versand-Intervall-Dateien")
+    mail_csv_backup(path)
+
+    export_csv = path + "\\info\\Versand_kombiniert.csv"
+    row_count = convert_csv_in_folder(path, export_csv)
+    print(f"Anzahl der Datei-Empfaenger-Kombinationen: {row_count}")
+    row_count = count_unique_emails(export_csv)
+    print(f"Anzahl der eindeutigen E-Mail-Adressen: {row_count}")
+    export_csv2 = path + "\\info\\Versand_kombiniert_komplett.csv"
+    convert_csv_in_folder(path, export_csv2, True)
+
+
+def workflow2():
+    convert_combined_to_folder("info\\Versand_kombiniert.csv", "export2")
+
+
+if __name__ == "__main__":
+    convert_folder_to_combined_csv()
+    workflow2()

+ 18 - 2
misc2.py

@@ -1,8 +1,12 @@
+import os
+
 import typer
 
 import config
 from misc import apache_ldap
+from misc.convert_mail_csv import convert_folder_to_combined_csv
 from misc.csv_cleanup import csv_cleanup
+from misc.datev_status import print_all_mdb_files
 from misc.headers import create_headerfiles
 
 app = typer.Typer()
@@ -47,5 +51,17 @@ def csv_check(path: str = ""):
     csv_cleanup(path)
 
 
-if __name__ == "__main__":
-    headers()
+@app.command()
+def combine_csv(path: str = ""):
+    if path == "":
+        path = f"{cfg.xml_dir}"
+    os.makedirs(path + "\\info", exist_ok=True)
+    os.makedirs(path + "\\backup", exist_ok=True)
+    convert_folder_to_combined_csv(path)
+
+
+@app.command()
+def datev_details(path: str = ""):
+    if path == "":
+        path = cfg.system_dir
+    print_all_mdb_files(path)