2

I used this code from this question here: Remove item from legend using ArcPy?

However I keep getting an error at the following line:

legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]

that says:

AttributeError: 'list' object has no attribute 'listLegendItemLayers'

However, I clearly have a legend in the mxd's.

import arcpy
import arcpy.mapping
import os
import sys
from arcpy import env
import string


env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
    for lyr in legend.listLegendItemLayers():
        if lyr.name == "atikot":
            legend.removeItem(lyr)
            print 'remove'
    mxd.save()
del mxd
1

I think you problem is in this line

mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)

try this

mxd = arcpy.mapping.MapDocument("C:/Project/" + mxdname)
1

Maybe replace :

legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
    for lyr in legend.listLegendItemLayers():

With the following:

legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
legendObject = legend[0]
    for lyr in legendObject.listLegendItemLayers():

I believe, possibly, that the legend object you've created by indexing the result of arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0] is actually still a list containing one object rather than the desired LegendElement object itself. This may be the reason why the Python interpreter is complaining about the call to method 'listLegendItemLayers' out of a list, where the method is actually onboard the LegendElement object held in the list.

  • I tried your suggestion, and got this error. Do you have any idea what it means? Thanks. legendObject = legend[0] TypeError: 'LegendElement' object does not support indexing – user3767931 Oct 14 '14 at 21:35
0

You have not included the version of ArcGIS for Desktop that you are using but the error message you are seeing is consistent with it being ArcGIS Desktop 10.0 because the listLegendItemLayers method was only added to the LegendElement Class at ArcGIS 10.1 for Desktop.

I am not aware of any workaround to doing this at 10.0 so I recommend upgrading to 10.1 or 10.2 (preferably) in order to run that script.

  • I'm using version 10.1. – user3767931 Oct 15 '14 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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