0

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
1

Since there is no way to change the default vb parser to say python you will have to use "\" and \"" in order to make the vb expression work in your arcpy script.

Here's the python lblClass.expression..

"\"<FNT name='Arial' size='12'>\"  &  [HOUSENUMBER] & \"</FNT>\""

Here's how it translate in the vb label expression in ArcMap..

"<FNT name='Arial' size='12'>"  &  [HOUSENUMBER] & "</FNT>"

Example:

import os
import arcpy # if outside ArcMap

# Label Expression format
# "\"<FNT name='Arial' size='12'>\"  &  [HOUSENUMBER] & \"</FNT>\""

# 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 = "\"<FNT name='Arial' size='12'>\"  &  [HOUSENUMBER] & \"</FNT>\""
            if lblClass.showClassLabels:
                print "    Class Name:  " + lblClass.className
                print "    Expression:  " + lblClass.expression

lyr.showLabels = True

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.