How do I specify which field acts as resource for labeling a layer using ArcPy?

Following code is some settings that I am doing on a standalone ArcPy snippet and as you can see I added layer.showLabels = True but assuming I have a field named name I do not know how to add it to the code.

layer = arcpy.mapping.Layer("Primary")
layer.minScale = 10000
layer.showLabels = True

Once you have the label class object set the expression using the expression property:

expression: Provides the ability to get or set a layer's individual label class expression. This can be as simple as a single field or more advanced using either a VBScript, JScript or Python expression.

for lblClass in lyr.labelClasses:
    if lblClass.showClassLabels:
        lblClass.expression = lblClass.expression = "\"<FNT name='Arial' size='12'>\"  &  [MyFieldNameHere] & \"</FNT>\""

As commented by @MichaelStimson:

You need to specify a labelClass object https://resources.arcgis.com/en/help/main/10.2/index.html#/LabelClass/00s30000002t000000/ - even if you have only one class, look at the example in the link.

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.