I can't remove item from the legend (lyr called "atikot") using arcpy with this code:
import arcpy
from arcpy import env
removeLyr = arcpy.mapping.ListLayers(mxd, "atikot")[0]
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
for lyr in legend.listLegendItemLayers():
if lyr.name == "atikot":
legend.removeItem(lyr)
del mxd
The code does not work. Does anyone have an idea as to what's wrong with the code?
Best Answer
I think the problem with your code is that you have not included an
mxd.save()
before deleting your map document (mxd) object at the end. You're also referring to yourmxd
variable before you have assigned it.Your modified code should look like this:
Make sure that you save the map document (mxd) object before you delete it.