Arcpy For Loop to Apply Symbology to Multiple Layers

4323
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
DanPatterson_Retired
MVP Emeritus

you retrieve the first layer in the data frame and are trying to apply the symbology from layer on disk.  Have you confirmed that the layers are of the same type so that the symbology will apply?

("C:\LayerStyles\LayerFile.lyr")  should be (r"C:\LayerStyles\LayerFile.lyr")  raw formatted with the "r" just be sure it can find the layer.  So, dump the try except block so you get a real error message, confirm that the layer can be found and use a check to ensure that the symbology from that layer is appropriate for the layer it is trying to apply it to.

WesMiller
Regular Contributor III

Change line 18 your using a list instead of  a layer

in_layer to lyr

ListLayers—Help | ArcGIS for Desktop

0 Kudos
DanPatterson_Retired
MVP Emeritus

in_layer = arcpy.mapping.ListLayers(mxd, "", df)[0]       [0] should mean he is getting the first layer in the list... if that is not what he wants, then the question is muddier

WesMiller
Regular Contributor III

Your right, I missed [0] on the end. So why the loop?

0 Kudos
DanPatterson_Retired
MVP Emeritus

line 7... the current mxd... but I think there is more fundamental to just fixing up a for loop which contains a try except block which doesn't return any useful information in its current form.

0 Kudos
DarrochKaye
Occasional Contributor

My apologies if my original post was unclear. The code I posted was my attempt based on other pieces of code I know work.

I want to apply the symbology that is stored in a layer file to all layers I currently have open in ArcMap in a Group Layer. They are all polygon files and all have the same fields so there is no error in that regard.

Thanks,

DK

0 Kudos
DarrochKaye
Occasional Contributor

So far I have got the following code to work on a single, specified layer:

import arcpy

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

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

# Layer to receive imported symbology.
in_layer = arcpy.mapping.ListLayers(mxd, "09-03-2016 00_00_00", df)[0]

arcpy.ApplySymbologyFromLayer_management(in_layer,in_symbology_layer)

# Refreshes Table of Contents.
arcpy.RefreshTOC()

...but what I would like is to apply this code to multiple layers, e.g. layers in a Group Layer, rather than a uniquely specified layer, e.g. "09-03-2016 00_00_00".

Thanks,

DK

0 Kudos
DanPatterson_Retired
MVP Emeritus
all_layers = arcpy.mapping.ListLayers(mxd, "09-03-2016 00_00_00", df)   # note ... no [0]
in_layer = all_layers[0]  # the first layer
for layer in all_layers:  
    arcpy.ApplySymbologyFromLayer_management(layer, in_symbology_layer)  # guessing since arc-free
DarrochKaye
Occasional Contributor

Solution:

import arcpy

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

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

# Layer to receive imported symbology.
all_layers = arcpy.mapping.ListLayers(mxd)

for layer in all_layers: 
    arcpy.ApplySymbologyFromLayer_management(layer, in_symbology_layer)

# Refreshes Table of Contents.
arcpy.RefreshTOC()

Thanks to all those who contributed.

DK

0 Kudos