diff --git a/README.md b/README.md index 8d45d1b..ef3f567 100644 --- a/README.md +++ b/README.md @@ -98,16 +98,51 @@ pip install pdfplumber icalendar pypdf2 pytz ## Verwendung -### Schnellstart +### Schnellstart (CLI) -1. Kopieren Sie Ihre Dienstplan-PDF-Dateien in dieses Verzeichnis +1. Kopieren Sie Ihre Dienstplan-PDF-Dateien in ein Verzeichnis 2. Führen Sie das Skript aus: ```bash python3 pdf_to_ics.py ``` -Das Tool findet automatisch alle `.pdf` Dateien und erstellt entsprechende `.ics` Dateien. +Das Tool findet automatisch alle `.pdf` Dateien im aktuellen Verzeichnis und erstellt entsprechende `.ics` Dateien. + +### Kommandozeilen-Optionen + +```bash +# Alle PDFs im aktuellen Verzeichnis konvertieren +python3 pdf_to_ics.py + +# PDFs aus einem bestimmten Verzeichnis konvertieren +python3 pdf_to_ics.py --input ./pdfs + +# PDFs in anderes Verzeichnis speichern +python3 pdf_to_ics.py --input ./pdfs --output ./ics_dateien + +# Ruhetage ausschließen +python3 pdf_to_ics.py --exclude-rest + +# Einzelne PDF-Datei konvertieren +python3 pdf_to_ics.py /pfad/zur/datei.pdf + +# Mit detaillierter Ausgabe +python3 pdf_to_ics.py --input ./pdfs -v + +# Hilfe anzeigen +python3 pdf_to_ics.py --help +``` + +**Verfügbare Optionen:** + +| Option | Kurzform | Beschreibung | +|--------|----------|-------------| +| `--input DIR` | `-i` | Eingabe-Verzeichnis mit PDF-Dateien (Standard: aktuelles Verzeichnis) | +| `--output DIR` | `-o` | Ausgabe-Verzeichnis für ICS-Dateien (Standard: Eingabe-Verzeichnis) | +| `--exclude-rest` | `-e` | Ruhetage ausschließen (Ruhe, R56, R36, vRWF48, RWE, vR48) | +| `--verbose` | `-v` | Detaillierte Ausgabe anzeigen | +| `--help` | `-h` | Hilfe anzeigen | ### Erweiterte Nutzung diff --git a/pdf_to_ics.py b/pdf_to_ics.py index 95d9962..2674203 100644 --- a/pdf_to_ics.py +++ b/pdf_to_ics.py @@ -255,39 +255,131 @@ def create_ics_from_dienstplan(dienstplan, output_path=None, exclude_rest=False) def main(): """ - Hauptfunktion + Hauptfunktion mit CLI-Argumenten """ import sys + import argparse - # Finde alle PDF-Dateien im aktuellen Verzeichnis - pdf_files = list(Path('.').glob('*.pdf')) + parser = argparse.ArgumentParser( + description='PDF zu ICS Konverter - Konvertiere Dienstplan-PDFs zu iCalendar-Dateien', + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=""" +Beispiele: + python3 pdf_to_ics.py # Konvertiere alle PDFs im aktuellen Verzeichnis + python3 pdf_to_ics.py --input ./pdfs --output ./ics # PDFs aus ./pdfs → ICS zu ./ics + python3 pdf_to_ics.py --input ./pdfs --exclude-rest # Schließe Ruhetage aus + python3 pdf_to_ics.py file.pdf # Konvertiere einzelne Datei + """ + ) + + parser.add_argument( + 'pdf_file', + nargs='?', + help='Einzelne PDF-Datei zum Konvertieren (optional)' + ) + + parser.add_argument( + '-i', '--input', + type=str, + default='.', + help='Eingabe-Verzeichnis mit PDF-Dateien (Standard: aktuelles Verzeichnis)' + ) + + parser.add_argument( + '-o', '--output', + type=str, + help='Ausgabe-Verzeichnis für ICS-Dateien (Standard: Eingabe-Verzeichnis)' + ) + + parser.add_argument( + '-e', '--exclude-rest', + action='store_true', + help='Ruhetage ausschließen (Ruhe, R56, R36, vRWF48, RWE, vR48)' + ) + + parser.add_argument( + '-v', '--verbose', + action='store_true', + help='Detaillierte Ausgabe' + ) + + args = parser.parse_args() + + # Bestimme Eingabe-Verzeichnis + if args.pdf_file: + # Einzelne Datei + pdf_file = Path(args.pdf_file) + if not pdf_file.exists(): + print(f"✗ Fehler: Datei nicht gefunden: {pdf_file}") + sys.exit(1) + pdf_files = [pdf_file] + input_dir = pdf_file.parent + else: + # Verzeichnis + input_dir = Path(args.input) + if not input_dir.exists(): + print(f"✗ Fehler: Verzeichnis nicht gefunden: {input_dir}") + sys.exit(1) + pdf_files = sorted(input_dir.glob('*.pdf')) + + # Bestimme Ausgabe-Verzeichnis + output_dir = Path(args.output) if args.output else input_dir + output_dir.mkdir(parents=True, exist_ok=True) if not pdf_files: - print("Keine PDF-Dateien gefunden!") + print(f"⚠ Keine PDF-Dateien gefunden in: {input_dir}") return + if args.verbose: + print(f"📂 Eingabe-Verzeichnis: {input_dir}") + print(f"📂 Ausgabe-Verzeichnis: {output_dir}") + print(f"📄 PDF-Dateien gefunden: {len(pdf_files)}") + if args.exclude_rest: + print("🧘 Ruhetage werden ausgeschlossen") + print() + + success_count = 0 + error_count = 0 + for pdf_file in pdf_files: - print(f"\nVerarbeite: {pdf_file}") + print(f"\n▶ Verarbeite: {pdf_file.name}") try: # Extrahiere Daten dienstplan = extract_dienstplan_data(str(pdf_file)) - print(f"Name: {dienstplan['vorname']} {dienstplan['name']}") - print(f"Personalnummer: {dienstplan['personalnummer']}") - print(f"Betriebshof: {dienstplan['betriebshof']}") - print(f"Anzahl der Events: {len(dienstplan['events'])}") + if args.verbose: + print(f" Name: {dienstplan['vorname']} {dienstplan['name']}") + print(f" Personalnummer: {dienstplan['personalnummer']}") + print(f" Betriebshof: {dienstplan['betriebshof']}") + print(f" Anzahl der Events: {len(dienstplan['events'])}") + + if not dienstplan['events']: + print(f" ⚠️ Warnung: Keine Events gefunden!") + error_count += 1 + continue # Erstelle ICS-Datei - ics_path = pdf_file.with_suffix('.ics') - create_ics_from_dienstplan(dienstplan, str(ics_path)) + ics_filename = pdf_file.stem + '.ics' + ics_path = output_dir / ics_filename + create_ics_from_dienstplan(dienstplan, str(ics_path), exclude_rest=args.exclude_rest) - print(f"✓ ICS-Datei erstellt: {ics_path}") + print(f" ✓ ICS erstellt: {ics_path}") + success_count += 1 except Exception as e: - print(f"✗ Fehler bei {pdf_file}: {e}") - import traceback - traceback.print_exc() + print(f" ✗ Fehler: {str(e)}") + if args.verbose: + import traceback + traceback.print_exc() + error_count += 1 + + # Zusammenfassung + print("\n" + "="*50) + print(f"✅ Fertig!") + print(f" Erfolgreich: {success_count}") + print(f" Fehler: {error_count}") + print("="*50) if __name__ == '__main__':