I have a point feature class for which I want to show labels based on a numeric "Field". My labels have to be rounded to the second decimal place and I need to have them padded with zeros. I know that I can manually modify it through the Layer Properties, but I need to write a script for that.
I have tried to run the following code, also based on other posts, but I end up with no labels at all:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "Layername")[0]
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
lblClass.className = "Field"
lblClass.expression = "Field".format("%.2f")
layer.showLabels = True
I wonder whether I have missed some steps. Thanks in advance for your help!
Solved! Go to Solution.
I think the reason why no labels are displaying is because that expression isn't valid in VBScript, which is the default label expression parser in ArcMap. It looks like a Python expression, but that format statement isn't quite right. Using the Python parser that expression is valid, but it would set all the labels to say "Field". What you'll probably want to do is use the VBScript FormatNumber function.
Other considerations:
If you're looking to label specific label classes in the layer, you could do something like this:
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
if(lblClass.className == "LabelThisClass"):
lblClass.expression = "FormatNumber([field],2)"
lblClass.showClassLabels = True
layer.showLabels = True
Otherwise, if all the features in the layer are labeled the same way you could do something like this:
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
lblClass.expression = "FormatNumber([field],2)"
layer.showLabels = True
This code uses VBScript for the label expression since that is the default parser. An equivalent Python expression could be written using the format() method: "format(float([field]),'.2f')". I don't think there's a way to change the label expression parser using ArcPy though, so you'll probably want to go with VBScript.
A potentially useful reference:
I think the reason why no labels are displaying is because that expression isn't valid in VBScript, which is the default label expression parser in ArcMap. It looks like a Python expression, but that format statement isn't quite right. Using the Python parser that expression is valid, but it would set all the labels to say "Field". What you'll probably want to do is use the VBScript FormatNumber function.
Other considerations:
If you're looking to label specific label classes in the layer, you could do something like this:
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
if(lblClass.className == "LabelThisClass"):
lblClass.expression = "FormatNumber([field],2)"
lblClass.showClassLabels = True
layer.showLabels = True
Otherwise, if all the features in the layer are labeled the same way you could do something like this:
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
lblClass.expression = "FormatNumber([field],2)"
layer.showLabels = True
This code uses VBScript for the label expression since that is the default parser. An equivalent Python expression could be written using the format() method: "format(float([field]),'.2f')". I don't think there's a way to change the label expression parser using ArcPy though, so you'll probably want to go with VBScript.
A potentially useful reference:
Thanks for your help! By the way, I have noticed that if I ran the code in ArcMap, it used the VBScript parser for the label expression, as you wrote. However, when I integrated it into a model, it appeared that through the model the parser was automatically set to Python. I have not found a proper explanation for that, though.
That's interesting. Maybe another tool in the model changed the parser to Python. I'm not sure if that's possible, but I tested the script on its own in Model Builder and it used the VBScript.