3

I have a Python assignment where I have to write a script to change the style of the north arrow from “ESRI North 4” to “ESRI North 1”. I wrote the following code:

import arcpy
mxd = arcpy.mapping.MapDocument(r"F:/Lab9/Georgia.mxd")

for elm in arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT"):

    if elm.name == "North Arrow":
        elm.style = "ESRI North 1"

mxd.save()

del mxd

It gave me no error, but It didn't change the existing style to the one I required.

  • 1
    Welcome to gis.SE. I'm not sure its appropriate to post purely academic material here, but perhaps we can provide some suggestions. For example, have you checked that the if elm.name test is ever true? If not, why might that be? – BradHards Nov 12 '13 at 1:36
3

I ran the test code below which is near identical to yours.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

for elm in arcpy.mapping.ListLayoutElements(mxd, "MAPSURROUND_ELEMENT"):
    print elm.name
    if elm.name == "North Arrow":
        print elm.style
        elm.style = "ESRI North 1"
mxd.save()

del mxd

What you will notice is that the print elm.style reports:

Traceback (most recent call last): File "C:/temp/test5.py", line 7, in print elm.style AttributeError: 'MapSurroundElement' object has no attribute 'style'

So the reason your code achieves nothing is that it simply sets a Python variable called elm.style to the value "ESRI North 1" rather than setting a non-existent property called style of an ArcPy MapSurroundElement object.

I do not know of a way to change the style of a North Arrow using ArcPy so, if no one else does either, then I recommend that someone create an ArcGIS Idea to have ArcPy provided with a writable property on its MapSurroundElement objects.

  • 2
    There is no such thing as the .style attribute on any arcpy.mapping layout element object. You can't just make up an API and pretend it will work. – Jason Scheirer Nov 12 '13 at 16:35
1

From everything I have seen, or more correctly not seen, while trying to find a way to change the color of my north arrows and scale bars, it appears it is still not possible to change the type or style of north arrow using arcpy.

Also, arcpy.mapping does not allow you to add or remove graphic elements, and cloned elements can only be applied to the mxd where the original object is found. So creating a dummy map document with every kind of north arrow you might use, and cloning the one you want will not work either.

An explanation for this is found in the discussion at http://forums.arcgis.com/threads/26979-Deleting-Layout-Elements-with-arcpy.mapping:

Our objective with arcpy.mapping was NOT to recreate ArcObjects capabilities but to simplify the scripting experience. If we gave you the ability to add new objects (e.g., scale bar) we would need to expose ALL of the properties associated with adding that object (aka ArcObjects).

Arcpy.mapping is intended to work with already authored mxds and layer files. The idea is that we use ArcMap and all of its 100's of buttons, menus, properties dialogs to author the content and we use arcpy.mapping to automate it.

As suggested by PolyGeo, this is something the community would have to request from ESRI in a future release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.