Altering label styles (font, color, size, etc.) using ArcPy?
The arcpy.mapping API only provides access to a limited number of layer properties that can be directly modified but all properties found in the Layer Properties dialog box can be modified using the UpdateLayer function. The information is extracted from a source layer and applied to the layer in a map document that needs to be updated. The source_layer can either be a layer (.lyr) file or a layer within a map document. UpdateLayer is a robust function because it can be used in several different ways to produce different results.
You need to create the symbology/labels how you want, and save it to a layer file. Then load that layer file using arcpy.mapping.UpdateLayer().
If multiple people need to run the python add-in, then you need to make the layer file accessible to everyone, such as a public network drive.
I ended up making a custom Update Layer Properties toolbox. It allows you to select a layer in the ArcMap TOC, and then select an input layer file. It then updates the layer with all the properties within the layer file, and re-sources back to the original feature class. It will work with data that's in gdb, mdb, shp, or sde.
View the Source Code
Please let me know if you have any questions.
Another option is to set custom label expression on the layer (changing size and color..etc) using python:
e.g.
def FindLabel ( [NAME], [POPULATION] ):
if long([POPULATION]) >= 250000:
return "<CLR red='255'><FNT size = '14'>" + [NAME] + "</FNT></CLR>"
else:
return [NAME]
Building label expressions
They may be set via:
LabelClass (arcpy.mapping)
EDIT:
Try this:
def FindLabel ([Code], [Type]):
if [Type] == "MRR":
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
elif [Type] == "TAX":
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
elif [Type] == "MRT":
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
elif [Type] == "GSS":
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
elif [Type] == "TGS":
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
else:
return "<FNT name = 'Arial' size = '12'>" + Code + "</FNT>"
You can remove your other line statements (for loop, labClass, and call to function). Also, the function needs to be called FindLabel.