1

I want to use arcpy.mapping to make some changes to a mxd. I am using the Spyder environment for python. To change the title I can use the following code:

import arcpy

myMap = r'C:\path_to_mxd\myfile.mxd'
mxd = arcpy.mapping.MapDocument(myMap)
mxd.relativePaths = True
mxd.title = 'Title'
arcpy.RefreshActiveView()
mxd.save()

I can't execute mxd.save() on an open mxd file though. Is there a way to make changes to an open file and view any code I execute on the fly?

  • You seem to be missing mxd.save(). – PolyGeo Feb 3 '17 at 20:41
  • saving it shouldn't affect the refresh, I should still see a change in arcmap without saving – Stefano Potter Feb 3 '17 at 20:42
  • Not if you are running your code from an IDE rather than through ArcMap. It sounds like you are doing the former. If not, can you edit your question to provide more details about how you are running your code, please? – PolyGeo Feb 3 '17 at 20:51
  • ok, yes you are correct i am using an IDE. adding mxd.save() returns 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 access and plenty of space. – Stefano Potter Feb 3 '17 at 20:54
  • 1
    Your question seems to be changing so now, while there are no answers, is the time to heavily revise it. – PolyGeo Feb 3 '17 at 20:58
4

You cannot save an open MXD from a python IDE external to ArcGIS.

You can, however, make changes to and save an MXD using Python from ArcMap itself. Open the python window (or you can attach to a script tool if you prefer) and run your python from inside the open MXD. You can display the python window from the menus Geoprocessing > Python Window.

You need to indicate to arcpy that you are looking at the open MXD by using arcpy.mapping.MapDocument("CURRENT")

mxd = arcpy.mapping.MapDocument("CURRENT")
mxd.relativePaths = True
mxd.title = 'Title'
arcpy.RefreshActiveView()
mxd.save()
1

The arcpy.mapping module was not designed to be used for the use case that you describe. A relevant post by Esri staff member Jeff Barrette at GeoNet says this better than I can:

Arcpy.mapping was designed for automating map documents (mxds) and layer files (lyrs), not the ArcMap application.

The functionality you are requesting is crossing into the realm of desktop customization and that is what ArcObjects is for.

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.