AnsweredAssumed Answered

Legend style in arcpy.mapping - Index Error: 0

Question asked by wle0001 on Sep 27, 2012
Latest reply on Oct 25, 2014 by curtvprice



I am currently working in ArcMap 10.1. I am trying to add layers to a map, then change the legend's style and location. Below is a sample code. legend variable is working, and I can move it around as a LayoutElement. However, when I want to update the style I keep getting an Index Error: 0. I've been trying my own custom style, but can't even get it to work with the


Here's the code:



import arcpy from arcpy import env arcpy.env.overwriteOutput = True import os  mxd = arcpy.mapping.MapDocument ("C:\\TestFolder\\Untitled.mxd") df = arcpy.mapping.ListDataFrames (mxd, "Layers")[0]  style = "" lyr1 = arcpy.mapping.Layer("C:\\TestFolder\\GriDSSAT_Counties.lyr") lyr2 = arcpy.mapping.Layer("C:\\TestFolder\\TestLayer\\LGI_20120821.lyr") lyr3 = arcpy.mapping.Layer("C:\\TestFolder\\States.lyr") styleItem = arcpy.mapping.ListStyleItems(style, "Legend Items")[0] legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0] legend.autoAdd = False arcpy.mapping.AddLayer(df, lyr1) legend.autoAdd = True arcpy.mapping.AddLayer(df, lyr2, "BOTTOM") legend.autoAdd = False arcpy.mapping.AddLayer(df, lyr3, "BOTTOM")  print styleItem  legend.updateItem(lyr2, styleItem)   legend.title = "Whoala"    arcpy.mapping.ExportToPDF (mxd, "C:\\TestFolder\\PDFFolder\\Test.pdf")



The error message I receive is as follows:


Traceback (most recent call last):
  File "C:\TestFolder\Scripts\", line 24, in <module>
    legend.updateItem(lyr2, styleItem)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\", line 772, in updateItem
    return convertArcObjectToPythonObject(self._arc_object.updateItem(*gp_fixargs((legend_item_layer, legend_item_style_item, preserve_style_sizes, use_visible_extent, show_feature_count, use_ddp_extent, index), True)))
IndexError: 0


Thanks for any insight on how to fix this




Message was edited by: Curtis Price 2014.10.25 typo in title