Select to view content in your preferred language

Labeling with python

2134
3
09-01-2013 06:22 PM
HitomiMaezawa1
Emerging Contributor
Hello,

I am trying to give features labels with python code.

Here is the code I am using:

>>> import arcpy
... mxd = arcpy.mapping.MapDocument("CURRENT") 
... layer = arcpy.mapping.ListLayers(mxd, "")[0] 
... if layer.supports("LABELCLASSES"):
...     for lblclass in layer.labelClasses:
...       lblclass.showClassLabels = True
... lblclass.expression = "[type]"
... layer.showLabels = True
... arcpy.RefreshActiveView()
... 


and I get this error:
Runtime error <type 'exceptions.NameError'>: name 'lblclass' is not defined

I thought this worked once, but not anymore..
Do I have to define "lblclass"??

If you could give me some help, I'd really appreciate it, thanks!
Tags (2)
0 Kudos
3 Replies
DaveBarrett
Deactivated User
Hi,

I have just tried your code in ArcMap in the python window and it worked fine with no errors. Are you still having problems with this or have you managed to get it work now?

Cheers

Dave
0 Kudos
HitomiMaezawa1
Emerging Contributor
Dave,

yeah it still does not work.

I should try with different approach..

Thanks for trying!
0 Kudos
markdenil
Frequent Contributor
There is a difference between SUPORTING label classes and
actually HAVING label classes.
If you are hitting a layer that supports, but does not have, label classes
then layer.labelClasses will not return any label classes,
let alone an itterable list of label classes.

You should test the result list of layer.labelClasses before attempting to use a member of the list.

As well, you have lblclass.expression = "[type]" unindented,
so even if the layer is skipped (for not supporting label classes)
you attempt to use the variable lblclass that is set inside the if statement,
regardless of if the if has failed (and not set the variable) or not.
0 Kudos