
Assigning a unique name to layout elements
It's a good practice to assign a unique name to all your layout elements using ArcMap. This is important in the event that your geoprocessing scripts need to access a particular element to make changes. For example, you might need to update the icon that displays your corporate logo. Rather than making this change manually in all your map document files, you could write a geoprocessing script that updates all your map document files programmatically with the new logo. However, in order for this to be possible, a unique name will need to be assigned to your layout elements. This gives you the ability to access the elements of your layout inpidually.
Getting ready
As I mentioned in the previous recipe, each layout element will be one of a number of element types and each can be assigned a name. This element name can then be used when you need to reference a particular element in your Python script. You can use ArcMap to assign unique names to each layout element. In this recipe, you will use ArcMap to assign names to the elements.
How to do it…
Follow these steps to learn how to assign unique names to each layout element using ArcMap:
- Open
C:\ArcpyBook\Ch4\Crime_Ch4.mxd
in ArcMap. - Switch to the layout view and you should see something similar to this screenshot:
- Names are assigned differently depending on the element type. Click on the uppermost data frame, which should be Crime, to select it. The selection handles should appear as follows:
- Right-click on the upper data frame and select Properties to display the Data Frame Properties window, as shown in the following screenshot. The Element Name property is what defines the unique name for the element and is found on the Size and Position tab as seen in the following screenshot. In this case, set the element name to
Crime
: - Close the Data Frame Properties window.
- Select 2009 Crime Data legend and open the Properties window by right-clicking on the legend and selecting Properties.
- Click on the Size and Position tab.
- Change the Element Name value to
Crime Legend
, as shown in the following screenshot: - You can also define unique names for text elements. Select the title element (
Crime and Its Impact on School Test Performance
), right-click on the element, and select Properties. - Select the Size and Position tab and define a unique name for this element, as shown in the following screenshot:
How it works…
Each element in the layout view can be assigned a name, which can then be used in your geoprocessing script to retrieve the specific element. You should strive to define unique names for each element. It isn't required that you define a unique name for each element, nor is it required that you even define a name at all. However, it is a best practice to give each element a name and ensure that each name is unique if you intend to access these elements from your Python scripts. In terms of naming practices for your elements, you should strive to include only letters and underscores in the name.
There's more…
You can use element names in conjunction with the ListLayoutElements()
function to restrict the elements that are returned by the function through the use of a wildcard parameter. In the next recipe, you'll learn how to restrict the list of layout elements that are returned through the use of wildcards and element types.