1
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd, "TestFile_LineToPoints13_Prod"):
    if lyr.supports("LABELCLASSES"):
        for lblclass in lyr.labelClasses:            
            lblclass.expression = " [OBJECTID]"
            if lblClass.showClassLabels:
                    print "    Class Name:  " + lblClass.className
                    print "    Expression:  " + lblClass.expression
mxd.save()
del mxd

Prints:

Class Name: Default

Expression: [Shape_Length]

The label for that layer is even set at ObjectID and is currently showing the labels... Why does it keep responding Shape_Length?

1 Answer 1

1

Try this code instead which I think will work because I tested it.

I suspect:

lblclass.expression = " [OBJECTID]"

should be:

lblclass.expression = "[OBJECTID]"

I also changed lblclass to lblClass in two places and used some more print statements while I was debugging.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd, "TestFile_LineToPoints13_Prod"):
    print lyr.name
    if lyr.supports("LABELCLASSES"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
            print lblClass.className
            lblClass.expression = "[OBJECTID]"
            if lblClass.showClassLabels:
                    print "    Class Name:  " + lblClass.className
                    print "    Expression:  " + lblClass.expression
mxd.save()
del mxd
3
  • When I added [0] I get a runtime error on line 3. Basically says AttributeError: Layerobject: Get attribute layers does not exist
    – Numus
    Commented Sep 16, 2014 at 23:41
  • @Numus I've added some tested code that I think should work better - adding [0] was unnecessary although I am surprised it threw that error.
    – PolyGeo
    Commented Sep 17, 2014 at 0:08
  • that seemed to work... It was probably the lower case class that was messing it up. Now I need to test it when I integrate it with my script but that won't be until tomorrow.
    – Numus
    Commented Sep 17, 2014 at 2:09

Your Answer

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

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