Moving Layout elements in ArcMap using Python

2347
6
06-10-2013 02:58 PM
MattBull
New Contributor II
I'm having some severe difficulties trying to move graphics and text elements in layout using Python. I figured it would be pretty simple, just access the layout elements and change the anchor point. I copied this code from the help and changed it to reflect the filepath and element name in my map document:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for elm in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT"):
    if elm.name == "Title Block":
        elm.elementPositionX = 4.75
        elm.elementPositionY = 10.5
mxd.save()
del mxd

When I run it through a Python script tool or the Python window, I get this error message:

IOError: MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

I have read/write privileges and there is certainly enough disk space, so I don't know what the problem is. I tried commenting out the last two lines of code and it ran without errors, but the element I was trying to use didn't move. I suspect there is something very simple and silly happening here that I am missing.
Tags (2)
0 Kudos
6 Replies
RhettZufelt
MVP Frequent Contributor
Normally get that error when the mxd is being viewed in ArcMap or ArcCatalog at the time the script runs.

Can't just close file either, need to exit ArcMap/Catalog before you run the script.  Also, look in task manager and make sure there isn't a "runaway" arcmap that is "locking" the file.

R_
0 Kudos
MattBull
New Contributor II
Thank you both, that is valuable information. Using the CURRENT keyword did exactly what I wanted the script to do, but it's also good to know that I need to be running the script outside of ArcGIS if I'm going to use the file path.
0 Kudos
AndrewBrachman1
New Contributor
I am having some trouble with a similar issue in moving an element.  For some reason the code is passing right by the FOR loop.  I am guessing it is really simple.  Thanks.



import arcpy

# Open RigMap.mxd
mxd = arcpy.mapping.MapDocument(r"D:\Temp\TestRigMapPython\UnitedStates_Basemap3.mxd")

# Export RigMap to JPEG
arcpy.mapping.ExportToJPEG(mxd, "D:\Temp\TestRigMapPython\TestRigMap.jpg", resolution = 300)

# Move text off of map area
for graphic in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT"):
    if graphic.name == "Statistics":
        graphic.elementPositionX = 4.4688
        graphic.elementPositionY = -0.505

# Export 30 Day PermitMap to JPEG
arcpy.mapping.ExportToJPEG(mxd, "D:\Temp\TestRigMapPython\Test30DayPermitMap.jpg", resolution = 300)
0 Kudos
NeilAyres
MVP Alum
Andrew,
perhaps its not finding a graphic element called "Statistics".
Double check that.
Cheers,
N
0 Kudos
AndrewBrachman1
New Contributor
I did check that and that doesn't seem to be the problem. 

One thing I tried was to put a print statement right inside of the for loop.  It never reaches the for loop so I am not sure what is going on.
0 Kudos
AndrewBrachman1
New Contributor
I figured it out.  I should have been using a TEXT_ELEMENT instead of a GRAPHIC_ELEMENT.

Silly mistake, sorry.
0 Kudos