Adding a scale and changing its position

2694
5
03-25-2013 08:05 AM
courtneygarlock
New Contributor II
I'm having trouble getting this script to run. I get errors saying line 4-list index out of range, and when I comment out the [0] I get an error saying 'list' object has no attribute 'parentDataFrameName' 

I'm trying to actually add the scale bar and add in titles, author etc... with python but I was first trying to get an already inserted scale bar to change its position.

Please help


>>>
#The following script will find the mapsurround element named ScaleBar and change it's position.
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Courtney\TEST.mxd")
scaleBar = arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT", "ScaleBar") [0]
df = arcpy.mapping.ListDataFrames(mxd, scaleBar.parentDataFrameName)[0]
scaleBar.elementPositionX = df.elementPositionX + (df.elementWidth / 2)
mxd.save()
del mxd
Tags (2)
0 Kudos
5 Replies
LucasDanzinger
Esri Frequent Contributor
If you loop through your ListLayoutElements variable, I bet you don't have anything in that list. This is why grabbing an index doesn't work and why grabbing a property of the MapsurroundElement object doesn't work.

My guess is that your issue is with the text "ScaleBar" wildcard. Do you have anything in your layout with the name ScaleBar? Not all scale bars are named "ScaleBar", but rather, are named closer to what the text says when you click Insert > Scale Bar, and select a scale bar here (i.e. "Scale Line", "Stepped Scale Line", "Alternating Scale Bar", etc.)

As a test, try the following to figure out what the name of your scale bar element is, and then you can use that as a wildcard in your code:

scaleBar = arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT")
for item in scaleBar:
    print item.name
0 Kudos
courtneygarlock
New Contributor II
The scale bar was "Alternating scale bar" I changed the element name to scale bar.
I can change the position but is it possible to use python code to add the scale bar to the layout. I haven't found this to be possible. I can only change its properties once it is added thru arcmap.

Is there a simple script that can add and modify  title, author, dataframetime etc...

I'm using this code:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Courtney\TEST1.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if elm.name == "TitleBlock":
        elm.text = '<dyn type="document" property="title"/>'
        elm.elementPositionX = 3.0
        elm.elementPositionY = 9.0
        elmfontSize = 28

and changing the elm.text to = author, title, dataframtime etc....

But I'm looking for a script that can add these elements in regards to eachothers position so I don't have to manually set the xy
positions
0 Kudos
LucasDanzinger
Esri Frequent Contributor
No there won't be python syntax to add the scale bar to the map. Broadly speaking, the mapping module is for manipulating existing maps and not for building new maps, so typically you will need to build a template map (i.e. a map with all of your layers and layout elements), and you can then manipulate that to your liking and save a copy to a new MXD.
Rohit_Venkat_GandhiMendadhala4
New Contributor

Hi Lucas,

Can you please let me know if we can change the existing style of a scale bar or North Arrow to another style in a layout programmatically using Python. I have searched all over the internet but could not find a methodology for this. Hope you can help me out.

0 Kudos
LukeWebb
Occasional Contributor III

You cant do what you asked.

You can however achieve the exact same results by:

Add 10 scale bars with unique names 'Off page', in different styles and stuff, and then use python to move them onto the

page as needed for different maps.

OR 

Create 10 "MXD" templates with different page setups, and use the code to determine which template to use.

OR

Think of something else ..be creative!

0 Kudos