I have an attribute table with some fields and I want to display on map the values of a field of this table. Table is a shapefile. I need to turn on label of a field automatically with arcpy, so I tried to use contour annotation but it gives error.

Field= "Teste"
env.workspace = "F:/ArcGIS/Trab/Model/Subestações.shp"
arcpy.ContourAnnotation_cartography('Subestações', env.workspace, Field , 50000, "ContourTeste", "BLACK", 
"", "PAGE", "")

ERROR 000366: Invalid geometry type

I could to use this function for a polyline feature, and with this error I think that problem is this function can't be used with point feature.

How I can turn on label of a field with arcpy?

  • When asking about errors from ArcPy can you always provide the code snippet that produced any error messages that you present, please? Turning on labels is probably the showLabels property of a Layer object. – PolyGeo Sep 7 '16 at 11:44
  • I already I edited my answer @PolyGeo – S.Rai Sep 7 '16 at 11:53
  • 1
    A shapefile has no label; only a layer does. You will need to address either a layer file or a layer in a map. See this similar question. – Tom Sep 7 '16 at 15:14
  • When I tried to run your code snippet using one of my shapefiles it gave an error: NameError: name 'env' is not defined In this case I could make an assumption about what one of the earlier lines in your script may have been but I think this illustrates that you have not provided us with a tested code snippet that we can just copy/paste and then run against our own test data. If you left out that line I always wonder what other lines that may be critical to your problem have also been left out. – PolyGeo Sep 7 '16 at 22:28

If I understand your question correctly you want to display labels in a shapefile from a specific field in your attribute table. Use this function.

First you need to use the arcpy.MakeFeatureLayer_management() tool to turn your shapefile into a layer so it can be brought into arc

 def labell(wah, field):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == wah:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = '"{}" + {} +  "{}"'.format("<CLR red='255' green='255' blue='255'><FNT size = '8'>", field, "</FNT></CLR>")
                    lblClass.showClassLabels = True
                    lyr.showLabels = True

labell(yourlayer, attributefield)

Not the answer you're looking for? Browse other questions tagged or ask your own question.