Select to view content in your preferred language

Batch Apply Symbology from one layer to another in multiple maps within an aprx

213
3
07-09-2025 01:13 PM
Labels (2)
Ron_S1
by
Emerging Contributor

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()

 

0 Kudos
3 Replies
RTPL_AU
Honored Contributor

I use XTools to copy / paste symbology , def queries, etc (when 3.5.1 allows me to copy anything without freezing) as needed from one layer to many other layers so the concept definitely should work if it is exposed properly. 

Not a developer so any suggestions may be well off track but is your source layer in a group by any chance?

0 Kudos
Melissa_B
Regular Contributor

There are a few bugs for this issue:

https://support.esri.com/en-us/bug/applysymbologyfromlayer-in-a-python-script-in-arcgis-pr-bug-00011...

https://support.esri.com/en-us/bug/running-the-apply-layer-symbology-tool-as-a-script-in-t-bug-00010...

https://support.esri.com/en-us/bug/the-apply-symbology-from-layer-tool-does-not-apply-symb-bug-00010...

I ran into the same problem a few months back making a similar script that used a layer file that acted as a symbology template and applied that symbology to every layer of a specific name in every map of a project.  I haven't tried it recently after updating to ArcGIS Pro 3.5.2 to confirm if it's fixed.

0 Kudos
Melissa_B
Regular Contributor

Just tested in ArcGIS Pro 3.5.2 - running it from a Jupyter Notebook successfully changed the symbology for every layer in every map without having to close and re-open the project.  Running the same script as a tool from a toolbox however still did not change the symbology properly for any layer in any map even when configured as derived output like described here: https://community.esri.com/t5/arcgis-pro-questions/applysymbologyfromlayer-toolbox-python-script/td-...

and here: https://community.esri.com/t5/python-questions/cannot-apply-symbology-from-layer-using-python/td-p/1...

But maybe I'm not understanding how the script tool output is handled. 

 

0 Kudos