import os import xml.etree.ElementTree as ET from pathlib import Path import shutil def convertir_a_current_color(archivo_entrada, archivo_salida=None, backup=True): """ Convierte todos los colores fijos en un SVG a currentColor. Preserva las opacidades y transparencias. """ if archivo_salida is None: path = Path(archivo_entrada) archivo_salida = path.parent / f"{path.stem}_color{path.suffix}" try: # Leer el archivo with open(archivo_entrada, 'r', encoding='utf-8') as f: contenido = f.read() # Parsear XML tree = ET.ElementTree(ET.fromstring(contenido)) root = tree.getroot() # Registrar namespace ET.register_namespace('', 'http://www.w3.org/2000/svg') # Procesar TODOS los elementos con fill o stroke elementos_modificados = 0 for elem in root.iter(): # Procesar fill if 'fill' in elem.attrib: fill = elem.get('fill') # Si no es none, ni currentColor, ni url(), reemplazar if fill and fill.lower() not in ['none', 'currentcolor'] and not fill.startswith('url'): elem.set('fill', 'currentColor') elementos_modificados += 1 # Procesar stroke if 'stroke' in elem.attrib: stroke = elem.get('stroke') if stroke and stroke.lower() not in ['none', 'currentcolor'] and not stroke.startswith('url'): elem.set('stroke', 'currentColor') elementos_modificados += 1 # Limpiar metadata innecesaria (opcional) for metadata in root.findall('.//metadata'): parent = root.find('.//g') or root if parent is not None: parent.remove(metadata) # Guardar el archivo tree.write(archivo_salida, encoding='utf-8', xml_declaration=True) print(f"✅ {Path(archivo_entrada).name} → {elementos_modificados} elementos convertidos") return True except Exception as e: print(f"❌ Error con {archivo_entrada}: {str(e)}") return False def procesar_carpeta(carpeta): """ Procesa todos los SVG en una carpeta """ carpeta_path = Path(carpeta) if not carpeta_path.exists(): print(f"❌ La carpeta {carpeta} no existe") return # Buscar todos los SVG (excepto los que ya tienen _color) archivos = [f for f in carpeta_path.glob('*.svg') if '_color' not in f.name] if not archivos: print(f"⚠️ No se encontraron archivos SVG en {carpeta}") return print(f"🔍 Encontrados {len(archivos)} archivos") print("=" * 50) # Crear carpeta de backup backup_dir = carpeta_path / 'backup_originales' backup_dir.mkdir(exist_ok=True) for archivo in archivos: # Hacer backup shutil.copy2(archivo, backup_dir / archivo.name) # Generar nombre de salida (sobrescribir el original o crear nuevo) # Opción 1: Sobrescribir el original (CUIDADO) # salida = archivo # Opción 2: Crear nuevo archivo con sufijo _color (RECOMENDADO) salida = archivo.parent / f"{archivo.stem}_color{archivo.suffix}" convertir_a_current_color(str(archivo), str(salida)) print("=" * 50) print(f"💾 Backups guardados en: {backup_dir}") print(f"📁 Archivos convertidos con sufijo '_color'") # ============= EJECUTAR ============= if __name__ == "__main__": # Cambia esta ruta a tu carpeta de iconos CARPETA = "./iconos" # Tu ruta procesar_carpeta(CARPETA)