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
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.
Well, I just went in to the MXD and changed the label expression there.
To emulate what you describe doing in the GUI, look into the labelClasses property of a layer object.
Will do, thanks!