AnsweredAssumed Answered

Arcpy For Loop to Apply Symbology to Multiple Layers

Question asked by darrochkaye on Mar 14, 2016
Latest reply on Jul 19, 2016 by sheilabr@usc.edu

Hi,

 

I have multiple layers in ArcMap that I want to import a symbology for from a layer file. The code I have is as follows:

 

import arcpy
from arcpy import env

# Reference current MXD and dataframe. 
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd,"",df)

# Layer file symbology to be imported is in.
in_symbology_layer = arcpy.mapping.Layer("C:\LayerStyles\LayerFile.lyr")

# Layer to receive imported symbology.
in_layer = arcpy.mapping.ListLayers(mxd, "", df)[0]

# For loop to import symbology to all layers in current MXD.
for lyr in layers:
  try:
    arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer)
  except:
    print "Failed to import symbology for: " +lyr.name

# Refreshes Table of Contents.
arcpy.RefreshTOC()

 

The code seems to work in parts, as the output I get is:

 

"Failed to import symbology for: Layer1

"Failed to import symbology for: Layer2

"Failed to import symbology for: Layer3..."

 

For loops are my Achilles Heel so any assistance would be greatly appreciated!

 

Thanks,

DK

Outcomes