0

I am outputting a set of maps with various layers and baselayers. On some of these I need to turn off the labels to reduce the noise and to see the baselayer with no obstructions.

I cant seem to figure out a way to turn off the labels for only the selected output maps. I have tried multiple variations of this code to no avail

lyrList = ['fields']
for lyr in lyrList:
     if lyr.supports("LABELCLASSES"):
          lyr = arcpy.mapping.ListLayers(mxd, lyr, df)[0]
          lyr.showLables = False
          arcpy.RefreshActiveView()

edit: to show lyrList is strings

4
  • 1
    Please share the lyrList. If it is a collection of layer objects vs a collection of strings (layer names) determines the script that follows.
    – Keggering
    Oct 14, 2021 at 22:12
  • 1
    Please start your code snippet with import arcpy (unless your running it from the Python Window, which does that automatically).
    – PolyGeo
    Oct 14, 2021 at 22:37
  • 1
    What errors (if any) did you get when running that code? Oct 14, 2021 at 22:46
  • @Keggering i added the lyrList I am using. I have tried many variations of this list. I'm not a programmer so I'm basically just stumbling blind. Oct 15, 2021 at 11:07

2 Answers 2

2

The Layer class of arcpy.mapping does not have a property named showLables [sic] but it does have one named showLabels. I would change the code that you have presented to be:

for lyr in lyrList:
     if lyr.supports("LABELCLASSES"):
          lyr = arcpy.mapping.ListLayers(mxd, lyr, df)[0]
          lyr.showLabels = False  # spelling of property name corrected here
arcpy.RefreshActiveView()

In addition to correcting the spelling of the property name, I also moved arcpy.RefreshActiveView() outside of loop so that the display only needs to be refreshed once, instead of once for every layer that supports label classes.

0
0

I suspect lyrList is a collection of name-strings (e.g. lyrList = ["Layer1", "Layer2",]). If this is the case, the string-type item doesn't work here if lyr.supports("LABELCLASSES"):.

Alternatively, lyrList is a collection layer Objects (e.g. lyrList = [<map layer u'SomeLayerName'>, <map layer u'SomeOtherLayerName'>] , then redefining a lyr object with lyr = arcpy.mapping.ListLayers(mxd, lyr, df)[0] doesn't work, as the "lyr" parameter should be a string (even if it worked, it's redundant).

Here is a snippet to ellaborate:

import arcpy

mxd = arcpy.mapping.MapDocument(r"CURRENT")### "CURRENT" or some path, e.g. r"C:\Project\Project.mxd"
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyrList = arcpy.mapping.ListLayers(mxd, "Build*", df)

print "Layer-List  = ", lyrList  ########## A simply list of things
print "Zeroth-Elm  = ", lyrList[0] ######## the zeroth element of the list
print "Zeroth-Type = ", type(lyrList[0])### type of the zeroth element

print "---"

for lyr in lyrList:
    print "Process '" + lyr.name + "' layer"
    if lyr.supports("LABELCLASSES"):
        ### not needed ---> lyr = arcpy.mapping.ListLayers(mxd, lyr, df)[0]
        lyr.showLabels = False
        print "\tShow-Labels = " + str(lyr.showLabels)

arcpy.RefreshActiveView()

Shown here: enter image description here

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.