import xml.etree.ElementTree as ET import os from collections import OrderedDict # Look in current directory input_dir = "." output_file = "screens_log.txt" # Ordered dictionary: {screen_id: list of sources} screens = OrderedDict() # Loop through all XML files in sorted filename order for file in sorted(os.listdir(input_dir)): if not file.lower().endswith(".xml"): continue path = os.path.join(input_dir, file) tree = ET.parse(path) root = tree.getroot() for screen in root.findall("SCREEN"): screen_id = screen.attrib.get("ID", "UNKNOWN").strip() if screen_id not in screens: screens[screen_id] = [] for src in screen.findall("SOURCE"): source_text = (src.text or "").strip() if source_text and source_text not in screens[screen_id]: screens[screen_id].append(source_text) # Write log grouped by screen ID with open(output_file, "w", encoding="utf-8") as f: for screen_id, sources in screens.items(): f.write(f"[{screen_id}]\n") for src in sources: f.write(f" {src}\n") f.write("\n") print(f"? Collected screens from all XMLs in current directory, keeping order by file and entry. Log written to {output_file}")