From 9471f9919c21b3ab8cbb6eef6cd472dd37c8f5a6 Mon Sep 17 00:00:00 2001 From: mistangl Date: Fri, 5 Dec 2025 18:53:48 +0100 Subject: [PATCH] =?UTF-8?q?es=20k=C3=B6nnen=20nun=20mehrere=20Ausgabeforma?= =?UTF-8?q?te=20bei=20der=20Extraktion=20auf=20einmal=20angegeben=20werden?= =?UTF-8?q?,=20z.B.=20text,json,excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/translate.py | 83 +++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/lib/translate.py b/lib/translate.py index 37dda90..11fb983 100644 --- a/lib/translate.py +++ b/lib/translate.py @@ -1154,8 +1154,7 @@ if __name__ == '__main__': '-t', '--export-type', action='store', default='json', - choices=['excel', 'json', 'text'], - help='Exportformat: json (Standard), excel (Excel-Datei) oder text (Textdatei mit Abschnitten)' + help='Exportformat(e): json (Standard), excel, text oder komma-separiert (z.B. excel,json,text)' ) parser.add_argument( '--translate', @@ -1179,6 +1178,19 @@ if __name__ == '__main__': args = parser.parse_args() + # Parse und validiere Export-Typen (komma-separierte Liste) + valid_export_types = {'excel', 'json', 'text'} + export_types = [t.strip() for t in args.export_type.split(',')] + + # Validiere jeden Export-Typ + for export_type in export_types: + if export_type not in valid_export_types: + parser.error(f"Ungültiger Export-Typ: '{export_type}'. Erlaubt sind: {', '.join(sorted(valid_export_types))}") + + # Entferne Duplikate und behalte Reihenfolge + seen = set() + export_types = [t for t in export_types if not (t in seen or seen.add(t))] + # Validierung: --extract benötigt --filename if args.extract and not args.filename: parser.error("--extract benötigt --filename") @@ -1371,37 +1383,42 @@ if __name__ == '__main__': # Schritt 2: Texte filtern texts, ignored_texts = filter_texts(all_texts, wildcard_patterns, regex_patterns, args.debug) - # Erstelle Output-Dateinamen basierend auf Export-Typ - if args.outname: - # Verwende benutzerdefinierten Namen - output_filename = args.outname - # Füge Endung hinzu falls nicht vorhanden - if args.export_type == 'excel': - if not output_filename.endswith('.xlsx'): - output_filename += '.xlsx' - elif args.export_type == 'json': - if not output_filename.endswith('.json'): - output_filename += '.json' + # Mapping für Dateiendungen + extension_map = { + 'excel': '.xlsx', + 'json': '.json', + 'text': '.txt' + } + + # Export für jeden angegebenen Typ durchführen + print(f"\nExportiere in {len(export_types)} Format(e): {', '.join(export_types)}") + + for export_type in export_types: + # Erstelle Output-Dateinamen basierend auf Export-Typ + if args.outname: + # Verwende benutzerdefinierten Namen + base_name = args.outname + # Entferne existierende Endung falls vorhanden + for ext in extension_map.values(): + if base_name.endswith(ext): + base_name = base_name[:-len(ext)] + break + # Füge korrekte Endung hinzu + output_filename = f"{base_name}{extension_map[export_type]}" + else: + # Standardname basierend auf Eingabedatei + output_filename = f"{filename.stem}_texts{extension_map[export_type]}" + + output_path = work_dir / output_filename + + # Schreibe Texte in gewähltem Format + if export_type == 'excel': + write_texts_to_excel(texts, output_path, translation_dicts) + elif export_type == 'json': + write_texts_to_json(texts, ignored_texts, output_path, translation_dicts) else: # text - if not output_filename.endswith('.txt'): - output_filename += '.txt' - else: - # Standardname basierend auf Eingabedatei - if args.export_type == 'excel': - output_filename = filename.stem + "_texts.xlsx" - elif args.export_type == 'json': - output_filename = filename.stem + "_texts.json" - else: # text - output_filename = filename.stem + "_texts.txt" + write_texts_to_text(texts, ignored_texts, output_path, translation_dicts) - output_path = work_dir / output_filename + print(f" [OK] {export_type}: {output_path}") - # Schreibe Texte in gewähltem Format - if args.export_type == 'excel': - write_texts_to_excel(texts, output_path, translation_dicts) - elif args.export_type == 'json': - write_texts_to_json(texts, ignored_texts, output_path, translation_dicts) - else: # text - write_texts_to_text(texts, ignored_texts, output_path, translation_dicts) - - print("Verarbeitung erfolgreich abgeschlossen.") + print("\nVerarbeitung erfolgreich abgeschlossen.")