1

Has anyone every had any issues with ArcGIS not loading the labels that are set via ArcPy? I've only inserted the code that labels two created layers that are made earlier in the script.

# uses the mxd that is running this code
mxd = arcpy.mapping.MapDocument("CURRENT")
# df is the dataframe, Layers is used to run through all the layers within the mxd. Leave Layers as is
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
# lyr sets the layer, needs to be spelt exactly as the layer sits in ArcMap
lyr = arcpy.mapping.ListLayers(mxd, "PARCELS_OWNER", df)[0]
arcpy.env.overwriteOutput = True

for lyr1 in arcpy.mapping.ListLayers(mxd, "Comps"):
    print lyr1.name
    if lyr1.supports("SHOWLABELS"):
        print lyr1.name + " supports label classes"
        for lblClass in lyr1.labelClasses:
            print lblClass.className
            lblClass.expression = lblClass.expression = '"{}" + "Parcel #:" + [AccountNo] + \n + [StrNum] + " " + [Street] + "{}"'.format("<FNT size = '18'>","</FNT>")
            if lblClass.showClassLabels:
                print "    Class Name:  " + lblClass.className
                print "    Expression:  " + lblClass.expression

        lyr1.showLabels = True

for lyr2 in arcpy.mapping.ListLayers(mxd, "Subject"):
     print lyr2.name
     if lyr2.supports("SHOWLABELS"):
        print lyr2.name + " supports label classes"
        for lblClass in lyr2.labelClasses:
             print lblClass.className
             lblClass.expression = lblClass.expression = '"{}" + "Subject #:" + [AccountNo] + \n + [StrNum] + " " + [Street] + "{}"'.format("<FNT size = '18'>","</FNT>")
             if lblClass.showClassLabels:
                print "    Class Name:  " + lblClass.className
                print "    Expression:  " + lblClass.expression

        lyr2.showLabels = True
del mxd
arcpy.RefreshActiveView()

I don't get any errors in my code, but the labels are never displayed. The box within properties is checked to display labels, but nothing.

  • 1
    Have you also checked the expression from within the label properties? Is it correct? If so, can you try using a simpler expression to test wether it's your code that is not doing things properly or the expression (e.g. lblClass.expression = 'TEST')? – umbe1987 Sep 10 at 15:47
  • I have tested it by doing something similar at it would show up. Could it be the label expression? – Taylor Kravits Sep 10 at 16:28
  • 2
    That's what I am saying. Try to test it directly in arcmap. If it does not work there, you foynd the issue. – umbe1987 Sep 10 at 18:27
  • 2
    The issue was with the newline python code, I switched it to VBnewline and it worked! – Taylor Kravits Sep 10 at 19:21
  • 1
    Glad you found out! You should post the solution as answer if you have time, would help anybody with a similar problem – umbe1987 Sep 10 at 20:26
0

I used VB to make this work.

for lyr in arcpy.mapping.ListLayers(mxd, "Comps"):
    print lyr.name
    if lyr.supports("SHOWLABELS"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
            print lblClass.className
            lblClass.expression = lblClass.expression = '"{}" & "Comp #: " & [AccountNo] & vbNewLine & [StrNum] & " " & [Street] & " " & [StrSuf] & " " & [StrUnit] & "{}"'.format("<FNT size = '10'>","</FNT>")
            if lblClass.showClassLabels:
                print "    Class Name:  " + lblClass.className
                print "    Expression:  " + lblClass.expression

        lyr.showLabels = True

for lyr in arcpy.mapping.ListLayers(mxd, "Subject"):
     print lyr.name
     if lyr.supports("SHOWLABELS"):
        print lyr.name + " supports label classes"
        for lblClass in lyr.labelClasses:
             print lblClass.className
             lblClass.expression = lblClass.expression = '"{}" & "Subject: " & [AccountNo] & vbNewLine & [StrNum] & " " & [Street] & " " & [StrSuf] & " " & [StrUnit] & "{}"'.format("<FNT size = '10'>","</FNT>")
             if lblClass.showClassLabels:
                print "    Class Name:  " + lblClass.className
                print "    Expression:  " + lblClass.expression

        lyr.showLabels = True
del mxd
arcpy.RefreshActiveView()

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.