[GIS] Changing style of north arrow “ESRI North 4” to “ESRI North 1” using ArcPy

arcpynorth-arrow

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.

Best Answer

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.

Related Question