Arcpy For Loop to Apply Symbology to Multiple Layers

4327
10
03-14-2016 08:39 AM
DarrochKaye
Occasional Contributor

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

Tags (3)
0 Kudos
10 Replies
SheilaBrown
New Contributor

Hi DK -

I used this script with great success on multiple mxds with several hundred layers. I would like to know if it is possible to apply this to mxds with group layers. I've looked in the forums but my python knowledge is not good enough to figure this out. Thanks.

Sheila

0 Kudos