StyleItem (arcpy.mapping)

Summary

Provides access to StyleItem class properties.

Discussion

The symbols you choose to display features, elements, or graphics in your map are stored in style files (.style). You use the Style Manager dialog box to create, view, and modify styles and their contents. Styles are organized into a defined set of categories or folder names, for example, Colors, Legend Items, Marker Symbols, and Scale Bars. Individual style items are stored in each category or folder name. For example, the Legend Items folder name has several different legend item style items, and each item has a different format, font size, and font type for the different elements that are displayed for a legend item.

The StyleItem class was introduced to serve a basic arcpy.mapping requirement to provide the ability to update legend item style items with custom settings. In the user interface, when you add a new layer to the table to contents, and that feature is automatically added to the legend, a default style is applied. The arcpy.mapping module allows you to update the individual legend item style items in a LegendElement on a page layout. This can be accomplished using the following workflow.

Properties

PropertyExplanationData Type
itemName
(Read Only)

A string that represents the name of the item the way it appears in the Style Manager dialog box window.

String
itemCategory
(Read Only)

A string that represents the category of the item the way it appears in the Style Manager dialog box window. Categories are used to group symbols within a style.

String
styleFolderName
(Read Only)

A string that represents the name of the folder the StyleItem is stored in, for example, Legend Items. Identifying the styleFolderName allows you to determine the type of the style object you have referenced.

String

Code Sample

StyleItem example

The following script uses the workflow outlined above and updates a legend's legend item style item. A layer is added to the first data frame in the map document and the legend item style item will be updated with a custom legend item style item called NewDefaultLegendStyle. The custom .style file is saved in the user's profile location.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.AddLayer(df, lyrFile, "TOP")
lyr = arcpy.mapping.ListLayers(mxd, 'Rivers', df)[0]
styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "NewDefaultLegendStyle")[0]
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.updateItem(lyr, styleItem)
del mxd
3/3/2014