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
arcpy.RefreshActiveView()