I am trying to automate MXD templates and want to bring layers in and label them based on attribute value without ever opening them in ArcMap. This means I cannot first open the layer symbology properties and manually add a second label class to modify with an ArcPy expression. The script below returns an error in line 7 of "list index out of range" - because the default labelling scheme is a single class not 2. If I have already created a second class in the layer the script works as intended.

Is there a way of changing the label method from the default scheme to multiple classes with ArcPy?

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("LABELCLASSES"):
                lcs = lyr.labelClasses
                Hydric = lcs[0]
                NonHydric = lcs[1]
                Hydric.SQLQuery = "hydclprs >=50"
                Hydric.expression = '"{}" + [MUSYM] + "{}"' .format("<BOL><FNT size='14'><CLR red='0' green='197' blue='255'>","</CLR></FNT></BOL>")
                NonHydric.SQLQuery = "hydclprs < 50"
                NonHydric.expression = '"{}" + [MUSYM] + "{}"' .format("<BOL><FNT size='14'><CLR red='255' green='170' blue='0'>","</CLR></FNT></BOL>")
                lyr.showClassLabels = True
                lyr.showLabels = True
  • Welcome to GIS SE. As a new user, please take the Tour. Note that, according to PEP8, you can choose any indent scheme you want, so long as you want 4 characters, and that class names should be leading uppercase, and variables lowercase. This will make your code more legible for others.
    – Vince
    Commented Feb 10, 2023 at 3:57

1 Answer 1


What you ask for, creating and inserting a new label class is not possible through arcpy. If you look at the help file for the Label Class object, there are no methods for creating one. You can only reference existing ones through the layer property labelClasses.

At best you could put the call to the second label class within a try/except statement and if it fails then log it for manual intervention?

  • I suspect you can do better than that, by applying symbology from layer file created with the required symbology, then altering it. I once created Python code to hack the binary stream of a template .lyr file, to alter the values used in the template to use the units I needed for the layer.
    – Vince
    Commented Feb 17, 2023 at 18:06

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.