Hi!
I am trying to develop a script to batch apply symbology from one source layer to another layer in multiple maps within an .aprx. I have a feature class that's being used in different map layouts, and I want to find a way to apply the symbology without having to go through each layout.
Attached below is my working script. It does run without error, but it does not do anything.
Thank you!
import arcpy
def main():
# Get parameters from ArcGIS Pro script tool
source_map_name = arcpy.GetParameterAsText(0) # Map with the styled source layer
source_layer_name = arcpy.GetParameterAsText(1) # Name of the styled layer
target_map_names = arcpy.GetParameterAsText(2).split(";") # Other maps to apply to
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Find the source map
source_map = next((m for m in aprx.listMaps() if m.name.strip().lower() == source_map_name.strip().lower()), None)
if not source_map:
arcpy.AddError(f"❌ Source map '{source_map_name}' not found.")
return
# Find the source layer
source_layer = next((lyr for lyr in source_map.listLayers() if lyr.name.strip().lower() == source_layer_name.strip().lower()), None)
if not source_layer:
arcpy.AddError(f"❌ Source layer '{source_layer_name}' not found in map '{source_map_name}'.")
return
symbology_applied = False # Flag to track if we made changes
for map_name in target_map_names:
m = next((m for m in aprx.listMaps() if m.name.strip().lower() == map_name.strip().lower()), None)
if not m:
arcpy.AddWarning(f"⚠ Map '{map_name}' not found. Skipping.")
continue
target_layer = next((lyr for lyr in m.listLayers() if lyr.name.strip().lower() == source_layer_name.strip().lower()), None)
arcpy.AddMessage(f" Found target layer '{target_layer.name}' in map '{map_name}'.")
if not target_layer:
arcpy.AddWarning(f"⚠ Layer '{source_layer_name}' not found in map '{map_name}'. Skipping.")
continue
try:
arcpy.management.ApplySymbologyFromLayer(target_layer, source_layer)
arcpy.AddMessage(f"✅ Symbology applied to layer '{source_layer_name}' in map '{map_name}'.")
symbology_applied = True
except Exception as e:
arcpy.AddWarning(f"❌ Failed to apply symbology in map '{map_name}': {e}")
if symbology_applied:
aprx.save()
arcpy.AddMessage(" Changes saved to project.")
if __name__ == "__main__":
main()