1

I'm using arcpy to create a GUI in Arcmap that can import features and apply symbology with importing a list of symbology layers, but it doesn't work and without an error message.

    for x in range (0, (len(list2))):
          print('shp', list2[x])
          print('symb', list3[x])
          arcpy.ApplySymbologyFromLayer_management(list2[x] , list3[x]) 
    arcpy.RefreshTOC()

list2: layers to apply symbology; list3: symbology layers

This is my code:

# Set outWorkspace
outWorkspace = r'c:\Users\HAIDRA\Documents\Desktop\essai'
mxd = arcpy.mapping.MapDocument("current")
style = askdirectory ()
styl_path.set(style)
# set the workspace
arcpy.env.workspace = style 
symbology_layers = arcpy.ListFiles("*")
print ('le premier boucle symbology_layers', symbology_layers)
list3=[]
print('list3', list3)
list3.append(symbology_layers)
print ('list3', list3)
dfs = arcpy.mapping.ListDataFrames(mxd)[0]
lyrs = arcpy.mapping.ListLayers(mxd, "", dfs)
print ('lyrs', lyrs)
list2=[]
print('list2', list2)
list2.append(lyrs)
print ('list2', list2)
for x in range (0, (len(list2))):
    print('shp', list2[x])
    print('symb', list3[x])
    arcpy.ApplySymbologyFromLayer_management(list2[x] , list3[x]) 
# Refresh the Table of Contents to reflect the change
arcpy.RefreshTOC()
6
  • 1
    How are list2 and list3 created? – GISGe Nov 3 '16 at 11:44
  • For list2, i listed layers existing in my mxd and i insert them in a list (with list2.append(lyrs)). And for the other list, the same thing, but symbology layers are existing in a folder that i opened it – Imen Dhifi Nov 3 '16 at 11:52
  • And could you show your code for this? It's difficult to find what's wrong without seeing it – GISGe Nov 3 '16 at 11:56
  • How can i insert a code in a comment please? – Imen Dhifi Nov 3 '16 at 11:59
  • Add it to your question – GISGe Nov 3 '16 at 12:01
1

You don't need to create list2 and list3, instead you can use lyrs and symbology_layers, respectively.

When using append, you add one new item to a list, so your list variable will contain only one item (a list):

list2 = [[lyrs[0], lyrs[1], ... [lyrs[x]]

Appending is not necessary since the result of the list functions is already a list.

I would personnaly use arcpy.mapping.UpdateLayer() to do what you're trying to do:

for x in range (0, (len(symbology_layers))):
    arcpy.mapping.UpdateLayer(dfs, lyrs[x], arcpy.mapping.Layer(symbology_layers[x]), 1)

EDIT

Another equivalent option, if you want to match layers based on their name (if corresponding layers have the same basename but a different extension) instead of their index in the list:

for x in lyrs:
    lyrname = x.name.split(".")[0]
    arcpy.mapping.UpdateLayer(dfs, x, arcpy.mapping.Layer(lyrname + ".lyr"), 1)
10
  • But it doesn't work too when i don't use list – Imen Dhifi Nov 3 '16 at 12:20
  • Ok, i'll try it – Imen Dhifi Nov 3 '16 at 12:31
  • It's work, but only to the first layer – Imen Dhifi Nov 3 '16 at 13:02
  • Makes sense, there was an error in my code, I've fixed it: replace symbology_layers[0] with symbology_layers[x] – GISGe Nov 3 '16 at 13:06
  • Yes,i try it, and it gives me the same result – Imen Dhifi Nov 3 '16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.