Tuesday 19 February 2019

arcpy - Why does ListLayoutElements give IndexError: list index out of range?


When trying to create a legend on ArcMap use the following Python/ArcPy code I received an error message:


import arcpy
mxd = arcpy.mapping.MapDocument(r"E:\cities\MyProject.mxd")

df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr1 = arcpy.mapping.Layer(r"E:\cities\cities.shp")
lyr2 = arcpy.mapping.Layer(r"E:\cities\region.shp")
lyr3 = arcpy.mapping.Layer(r"E:\cities\lakes.shp")
lyr4 = arcpy.mapping.Layer(r"E:\cities\rivers.shp")
legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
legend.autoAdd = True
arcpy.mapping.AddLayer(df, lyr1, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr2, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr3, "BOTTOM")

arcpy.mapping.AddLayer(df, lyr4, "BOTTOM")
legend.adjustColumnCount(2)
mxd.save()

and I get this message :


Traceback (most recent call last):
File "E:\cities\scripts\Lgend.py", line 8, in
legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
IndexError: list index out of range


What does this indicate?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...