convert_mail_csv.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import csv
  2. from pathlib import Path
  3. from misc.file_backup import copy_file_with_timestamp
  4. def count_unique_emails(csv_file):
  5. emails = set()
  6. with open(csv_file, "r") as file:
  7. reader = csv.reader(file, delimiter=";")
  8. for row in reader:
  9. if row[2] in ["Empfaenger", ""]:
  10. continue
  11. emails.add(row[2])
  12. with open(str(Path(csv_file).parent) + "\\email_empfaenger.csv", "w") as fwh:
  13. for email in sorted(emails):
  14. fwh.write(email + "\n")
  15. return len(emails)
  16. def convert_csv_in_folder(folder, export_csv, all_reports=False):
  17. row_count = 0
  18. with open(export_csv, "w") as fwh:
  19. fwh.write("Versand;Datei;Empfaenger\n")
  20. for file in Path(folder).glob("*.csv"):
  21. with file.open("r") as frh:
  22. reader = csv.reader(frh, delimiter=";")
  23. for row in reader:
  24. if len(row) < 2 or row[1] in ["Empfaenger"]:
  25. continue
  26. if row[1] == "":
  27. if all_reports:
  28. fwh.write(file.name + ";" + row[0] + ";\n")
  29. row_count += 1
  30. continue
  31. email_list = row[1].lower().split(",")
  32. for email in sorted(email_list):
  33. fwh.write(file.name + ";" + row[0] + ";" + email.strip() + "\n")
  34. row_count += 1
  35. return row_count
  36. def convert_combined_to_folder(csv_file, export_folder):
  37. result = {}
  38. with open(csv_file, "r") as frh:
  39. reader = csv.reader(frh, delimiter=";")
  40. for row in reader:
  41. if row[0] in ["Versand"]:
  42. # Kopfzeile ignorieren
  43. continue
  44. if row[0] not in result:
  45. result[row[0]] = {}
  46. if row[1] not in result[row[0]]:
  47. result[row[0]][row[1]] = []
  48. result[row[0]][row[1]].append(row[2])
  49. # if row[0] != current_file:
  50. # if fwh is not None:
  51. # fwh.close()
  52. # current_file = row[0]
  53. # fwh = open(f"{export_folder}\\{current_file}", "w")
  54. # fwh.write(row[1] + ";" + row[2] + "\n")
  55. # fwh.close()
  56. # json.dump(result, open("export2\\test.json", "w"), indent=2)
  57. for file, reports in result.items():
  58. print(file)
  59. with open(f"{export_folder}\\{file}", "w") as fwh:
  60. fwh.write("Datei;Empfaenger\n")
  61. for report, mails in reports.items():
  62. fwh.write(report + ";" + ",".join(sorted(mails)) + "\n")
  63. def mail_csv_backup(base_dir: str) -> None:
  64. for f in Path(base_dir).glob("*.csv"):
  65. # print("* " + f.name)
  66. copy_file_with_timestamp(str(f), str(f.parent) + "\\backup")
  67. def convert_mail_csv(csv_file):
  68. pass
  69. def convert_folder_to_combined_csv(path: str):
  70. print("Sicherung der Versand-Intervall-Dateien")
  71. mail_csv_backup(path)
  72. export_csv = path + "\\info\\Versand_kombiniert.csv"
  73. row_count = convert_csv_in_folder(path, export_csv)
  74. print(f"Anzahl der Datei-Empfaenger-Kombinationen: {row_count}")
  75. row_count = count_unique_emails(export_csv)
  76. print(f"Anzahl der eindeutigen E-Mail-Adressen: {row_count}")
  77. export_csv2 = path + "\\info\\Versand_kombiniert_komplett.csv"
  78. convert_csv_in_folder(path, export_csv2, True)
  79. def workflow2():
  80. convert_combined_to_folder("info\\Versand_kombiniert.csv", "export2")
  81. if __name__ == "__main__":
  82. convert_folder_to_combined_csv()
  83. workflow2()