I want to use ArcPy to turn on the labels for a layer.

I use this code but it fails and returns an error.

import arcpy   
mxd = arcpy.mapping.MapDocument(r"C:\Users\P51\Desktop\testingLABEL.mxd") 
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
layer = arcpy.mapping.ListLayers(mxd, "point10", "Layers")[0]  
layer.showLabels = True

This is the error :

Runtime error  Traceback (most recent call last):   
File "<string>", line 4, in <module>   
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)   
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1517, in ListLayers
    result = mixins.MapDocumentMixin(map_document_or_layer).listLayers(wildcard, data_frame)   File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\arcobjects\mixins.py", line 843, in listLayers
    raise TypeError(str(type(dataframe))) TypeError: <type 'str'>

Does anyone know how to fix this?

I have many layers (100 layers/feature class/ file geodatabase) and I want to label those layers then I convert those layers into annotation and after that I convert them to DWG CAD (point and annotation), but I want to do that automatically (especially when do labeling and turn on those label), because it will be wasting time if labeling those 100 layers 1 by 1.


1 Answer 1


If you look at the Syntax Help for ListLayers you will see that its third parameter is data_frame:

ListLayers (map_document_or_layer, {wildcard}, {data_frame})

and that data_frame is expected to be:

A variable that references a DataFrame object.

In your code you have:

layer = arcpy.mapping.ListLayers(mxd, "point10", "Layers")[0]

so you are trying to set data_frame to a string instead of a DataFrame object.

Your code already has a DataFrame object created when you wrote:

df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

so all you need to do is to use that instead of the string:

layer = arcpy.mapping.ListLayers(mxd, "point10", df)[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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