Print Out And Change Layer Labels With ArcPy

647
4
05-16-2017 09:15 AM
LloydBronn
Occasional Contributor II

I need to change the way my labels are displayed in ArcMap using ArcPy. I want to replace underscores with spaces in label strings with using string.replace("_"," "). I have SHOWLABELS set to True, but I'm having trouble accessing the label values themselves in ArcPy. Looking in the LabelClass documentation, I can't find any method to print out or alter the label values. 

city_layer = arcpy.mapping.ListLayers(mxd, "cities")[0]
countries_layer = arcpy.mapping.ListLayers(mxd, "countries")[0]
        
    if city_layer.supports("SHOWLABELS"):
        city_layer.showLabels = True

    if countries_layer.supports("SHOWLABELS"):
        countries_layer.showLabels = True

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

Do you mean the layer name in the TOC, which some people loosely call label, or do you mean labels of features in the map.  Labels of features are typically driven by data, either directly or through expressions.  If some text in a field is being used to label a feature, and that text has the underscore, then updating the text is the way to go.  If an expression is inserting the underscore, then modifying the expression is the way to go.

0 Kudos
LloydBronn
Occasional Contributor II

Well, I just went in to the MXD and changed the label expression there.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

To emulate what you describe doing in the GUI, look into the labelClasses property of a layer object.

0 Kudos
LloydBronn
Occasional Contributor II

Will do, thanks!

0 Kudos