From 85ef0c557468cb1c229dff62e13da18b22a87561 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Sat, 2 Jan 2021 20:42:48 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 +++ main.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7aa538 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +all.vcf +mannschaftslisten.csv +.vscode/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..9ea61ef --- /dev/null +++ b/main.py @@ -0,0 +1,66 @@ +import csv +import sys +from datetime import datetime +import phonenumbers + + +def convert(csvfile): + phone_rows = { + "HOME,CELL": "Mobil (Privat)", + "HOME,VOICE": "Festnetz (Privat)", + "WORK,CELL": "Mobil (Arbeit)", + } + with open(csvfile, "r", encoding="latin-1") as source: + reader = csv.DictReader(source) + for row in reader: + name = row["Name"] + firstname = row["Vorname"] + email = row["E-Mail"] + street = row["Strasse"] + postalcode = row["PLZ"] + place = row["Ort"] + title = row["Grad"] + notes = row["Zug"] + + birthday = datetime.strftime( + datetime.strptime(row["Geburtsdatum"], "%d.%m.%Y"), "%Y%m%d" + ) + + phones = "" + for tpe, col in phone_rows.items(): + if row[col]: + phonenumber_formatted = phonenumbers.format_number( + phonenumbers.parse(row[col], "CH"), + phonenumbers.PhoneNumberFormat.E164, + ) + phones = phones + f'TEL;TYPE="{tpe}":{phonenumber_formatted}\n' + + vcard = ( + "BEGIN:VCARD\n" + "VERSION:3.0\n" + f"N:{name};{firstname};;;\n" + f"FN:{firstname} {name}\n" + f"ORG:Feuerwehr Urdorf\n" + f"EMAIL;TYPE=HOME:{email}\n" + f"ADR;TYPE=HOME:;;{street};{place};;{postalcode};\n" + f"TITLE:{title}\n" + f"{phones}" + f"BDAY;VALUE=DATE:{birthday}\n" + f"NOTE:{notes}\n" + "CATEGORIES:Feuerwehr\n" + "END:VCARD\n" + ) + print(vcard) + + +def main(args): + if len(args) != 2: + print("Usage:") + print(args[0] + " filename") + return + + convert(args[1]) + + +if __name__ == "__main__": + main(sys.argv) \ No newline at end of file