I have a python script that I input into the label expression by hand which assigns different labels depending on the attribute data, see:

def FindLabel ( [a], [b] ):

if [b] != 'ok' and [b] != 'missing' and [b] != 'not transmitting':
  return "<CLR red = '255' green = '170'>" + [a] + " " + [b] + "</CLR>"
elif [b] == 'missing':
  return "<CLR red = '130' green = '130' blue = '130'>" + [a] + " " + "Missing" + "</CLR>"
elif [b] == 'not transmitting':
  return "<CLR red = '255'>" + [a] + " " + "Not Transmitting" + "</CLR>"
  return [a]

Is it possible to use arcpy to apply this label expression to a layer using labelClasses.expression, or any other method? The help page hints at the possibility by saying of the .expression method:

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.

but I've never seen a working version.


1 Answer 1


You can set the label classes from within the layer properties:

Select "Define classes of features and label each class differently" from the dropdown:

enter image description here

Add a class for each condition:

enter image description here

Set the SQL to filter out the records you want or don't want to label for each class

enter image description here

And then set the label expression for just that class:

enter image description here

The Classes and Label Expressions I used to match your python expression:

if [b] != 'ok' and [b] != 'missing' and [b] != 'not transmitting':

SQL Label Class = b not in ( 'missing', 'not transmitting', 'ok')
Label Expression = "<CLR red = '255' green = '170'>" + [a] + " " + [b] + "</CLR>"

elif [b] == 'missing':

SQL = b = 'missing'
Expression = "<CLR red = '130' green = '130' blue = '130'>" + [a] + " " + "Missing" + "</CLR>"

elif [b] == 'not transmitting':

SQL = b = 'not transmitting'
Expression = "<CLR red = '255'>" + [a] + " " + "Not Transmitting" + "</CLR>"


SQL = b = 'ok'
Expression = [a]

  • Sorry maybe I wasn't clear in my initial post. I have the labeling working perfectly when I access it manually through layer properties. I was wondering if there was a way to do it through arcpy in the python console window. May 14, 2018 at 18:37
  • @DanielMutton Arcpy allows you to modify existing label expressions, but it is not really possible to create complex label expressions like yours (you can't force the python parser or pass multi-line expressions). You could probably use arcpy to modify the simple ones from my answer though, if that's what you're wanting to do?
    – Midavalo
    May 14, 2018 at 19:12
  • Thank you for your help. I'm trying to automate as much of my map construction as I can, but if it isn't possibly to force the python parser then I'll just have to continue doing the labeling manually with my previous python expression. I'll give you the accepted answer as it seems my question was impossible. May 14, 2018 at 19:36

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.