# Define the maximum number of elements in the output directory max_elements = 6 import os from lxml import etree def extract_svg_elements(input_file, output_dir): # Parse the input XML file parser = etree.XMLParser(huge_tree=True) with open(input_file, "rb") as source: tree = etree.parse(source, parser=parser) root = tree.getroot() # Find all elements svgdoc_elements = root.xpath('//svgDoc') # Create a directory for the output files os.makedirs(output_dir, exist_ok=True) # Save each element to a separate file for i, svgdoc_element in enumerate(svgdoc_elements): if i >= max_elements: return svg_element = etree.fromstring(svgdoc_element.text, parser=parser) output_file = os.path.join(output_dir, f"svg_{i}.svg") with open(output_file, 'wb') as f: f.write(etree.tostring(svg_element, pretty_print=True)) if __name__ == "__main__": input_file_path = "NotoColorEmoji-Regular.ttx" # Replace with your actual input file path output_directory = "output_svgs" # Specify the output directory extract_svg_elements(input_file_path, output_directory) print(f"SVG elements extracted and saved in {output_directory}")