Select to view content in your preferred language

Writing to Map Document Properties with Python

1365
7
Jump to solution
11-28-2012 04:01 AM
RyanSutcliffe
Frequent Contributor
While working on some python scripts I quickly tried to erase the map title element via python using essentially the below code in a loop:

file = r"\\SUTCLIFFER-NEW\C$\Users\rsutcliffe\Desktop\Untitled.mxd" mxd = arcpy.mapping.MapDocument(file) mxd.title = '' #Additional lines tried mxd.save del mxd


Doing this returns no errors but once I recheck the mxd the values are not removed. This happens on every mxd I've tried/created. It seems I'm having trouble writing to this property: mxd.title. Is anyone able to spot what I'm missing?

I noticed the following posts:

The first is about trouble writing to the map document properties in a "Save As":
http://forums.arcgis.com/threads/29952-Accessing-Map-Document-Properties-via-arcpy.mapping?highlight...

The second is someone reportedly having success editing the property by running the code from within the mxd itself which I am also not able to do even though the code is pretty much the same.
http://forums.arcgis.com/threads/38202-Script-Error-Populating-mxd-document-properties-with-arcpy.ma...

any thoughts, advice are welcome. Thanks in advance.

Ryan Sutcliffe
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RyanSutcliffe
Frequent Contributor
I realized that the mxd.save command was not causing the save to occur and that I could actually affect the mxd title property within the map if I saved it manually after. Checked with ESRI Canada and the support person reminded me that the () after the mxd.save method was crucial and causing the issue.

Thus, the following code will do the trick:

file = r"\\SUTCLIFFER-NEW\C$\Users\rsutcliffe\Desktop\Untitled.mxd" mxd = arcpy.mapping.MapDocument(file) mxd.title = "" #Don't forget those brackets! mxd.save() del mxd


Simple syntax error. Thanks for your help Mathew, thanks for your help Jonothan H. at ESRI.

Ryan

View solution in original post

0 Kudos
7 Replies
MathewCoyle
Honored Contributor
Works fine for me on 10.0 SP3. What version/SP are you running?
0 Kudos
RyanSutcliffe
Frequent Contributor
I just switched to ArcGIS Desktop 10.1 Service Pack 1. When a machine gets available I'll see if I can replicate the issue on a 10.0 machine...

Ryan
0 Kudos
MathewCoyle
Honored Contributor
Worked fine for me in Desktop 10.1 SP1 as well. May be an issue with your mxd, have you tried it on more than one? Or have you tried making a new mxd and just putting in a title, saving it then seeing if you can modify it?
0 Kudos
RyanSutcliffe
Frequent Contributor
Thanks for testing on ArcGIS 10.1 for me. I have indeed tried on a bunch of mxds locally and on our server. I just created a new blank document now again and the same thing happens. I have the same issues for any of the map document properties like mxd.author. I just installed ArcGIS 10.1 so I'm going to test out a few other things and see if maybe something more general is wrong from the installation (python, read/write permissions). Unfortunately I don't have anyone else in office on 10.1 yet so can't just quickly test on another machine.
Will post back with what I find out.

Ryan

Ps. Just a note. I am running the above code in Pythonwin not from within ArcMap.
0 Kudos
MathewCoyle
Honored Contributor
I don't have pythonwin. I tested in IDLE and pyscripter.
0 Kudos
RyanSutcliffe
Frequent Contributor
I realized that the mxd.save command was not causing the save to occur and that I could actually affect the mxd title property within the map if I saved it manually after. Checked with ESRI Canada and the support person reminded me that the () after the mxd.save method was crucial and causing the issue.

Thus, the following code will do the trick:

file = r"\\SUTCLIFFER-NEW\C$\Users\rsutcliffe\Desktop\Untitled.mxd" mxd = arcpy.mapping.MapDocument(file) mxd.title = "" #Don't forget those brackets! mxd.save() del mxd


Simple syntax error. Thanks for your help Mathew, thanks for your help Jonothan H. at ESRI.

Ryan
0 Kudos
MathewCoyle
Honored Contributor
Must have had my blinders on, didn't even notice you left that out of the code you posted.
0 Kudos