I am trying to create a script that applies a layer style from .lyr file to layers in my table of contents that meet a certain criteria. The criteria being the layer name must contain string of "x".

Edit - This script now works for copying .lyr styles to layers that meet a certain if condition.

mxd = arcpy.mapping.MapDocument("CURRENT")
var1 = #Path to .lyr file
var2 = #Path to .lyr file

for lyr in arcpy.mapping.ListLayers(mxd):
    if "TEXT" in lyr.name:
        arcpy.ApplySymbologyFromLayer_management(lyr, var1)
        print lyr.name + " identified as TEXT"
    elif "TEXT2" in lyr.name:
        arcpy.ApplySymbologyFromLayer_management(lyr, var2)
        print lyr.name + " identified as TEXT2"
        print lyr.name + " did not meet the criteria."
  • 1
    Please roll back a bit further in your code and include the part where layer is defined, and include the diagnostic output of your script.
    – Vince
    Commented Apr 2, 2019 at 18:03
  • I incorrectly assumed layer would target the layers that meet the if condition. I'm trying to have my arcpy script look for "string" in variable then assign a lyr style to the layers that meet the in condition.
    – Chris
    Commented Apr 2, 2019 at 18:57
  • 1
    Did you get an error from Python, or was it masked inside a try block?
    – Vince
    Commented Apr 2, 2019 at 20:27
  • I think the first line in your for block should be to print lyr.dataSource, and the output from that should be part of what you report here.
    – PolyGeo
    Commented Apr 3, 2019 at 20:45
  • I was able to fix my script and it now works
    – Chris
    Commented Apr 3, 2019 at 21:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.