How can I use Arcpy to set label expression parser to Python?

4383
1
Jump to solution
01-28-2018 09:44 AM
JordanMiller5
New Contributor

I looked and looked but I can't find anything that lets the user to change the parser default to python for label expressions. The default is vb so when I run my arcpy script it list my python expression in the vb which doesn't work because it's in a different language. Can someone help me?

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Users\aa2zz6\Desktop\test\Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

for lyr in arcpy.mapping.ListLayers(mxd, "P_Meters"):
    print lyr.name
    if lyr.supports("LABELCLASSES"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
            print lblClass.className
            lblClass.expression = lblClass.expression = '"{}" + [HOUSENUMBER] +  "{}"'.format("<CLR red = '255' green = '255' blue = '255'><FNT size = '10'>","</FNT></CLR>") 
            if lblClass.showClassLabels:
                    print "    Class Name:  " + lblClass.className
                    print "    Expression:  " + lblClass.expression
                    
    lyr.showLabels = True
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Although the help (LabelClass—Help | ArcGIS Desktop) mentions this:

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.

.. I haven't seen a way of setting the parser. However, there isn't really a need to do so. You can simply use this and that will work:

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\GeoNet\LabelClass\myMap.mxd")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

for lyr in arcpy.mapping.ListLayers(mxd, "P_Meters"):
    print lyr.name
    if lyr.supports("LABELCLASSES"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
            print lblClass.className
            lblClass.expression = lblClass.expression = "\"<CLR red = '255' green = '255' blue = '255'><FNT size = '10'>\" & [HOUSENUMBER] & \"</FNT></CLR>\""
            if lblClass.showClassLabels:
                    print "    Class Name:  " + lblClass.className
                    print "    Expression:  " + lblClass.expression
        lyr.showLabels = True
mxd.save()
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

1 Reply
XanderBakker
Esri Esteemed Contributor

Although the help (LabelClass—Help | ArcGIS Desktop) mentions this:

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.

.. I haven't seen a way of setting the parser. However, there isn't really a need to do so. You can simply use this and that will work:

import os
import arcpy # if outside ArcMap

# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\GeoNet\LabelClass\myMap.mxd")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

for lyr in arcpy.mapping.ListLayers(mxd, "P_Meters"):
    print lyr.name
    if lyr.supports("LABELCLASSES"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
            print lblClass.className
            lblClass.expression = lblClass.expression = "\"<CLR red = '255' green = '255' blue = '255'><FNT size = '10'>\" & [HOUSENUMBER] & \"</FNT></CLR>\""
            if lblClass.showClassLabels:
                    print "    Class Name:  " + lblClass.className
                    print "    Expression:  " + lblClass.expression
        lyr.showLabels = True
mxd.save()
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍