I ran this script in PythonWin. I have folder(s) full of mxd documents that will require annual updates such as the year (date), which is what I was attempting here. This script ran but there were no changes made to the doc.
So, the original doc is in one folder and I want to change the date then save it in another folder. I've attached a PDF of the map I'm attempting to update. The year (2016) is in the title and signature. In the title, the year is grouped with the rest. That may be a problem?
Is the script wrong? Or is the title not properly represented in the text string?
Thanks in advance.
I've done this for hundreds of mxds using a script like below:
import arcpy, os
workspace = r'C:\blahblahblah' # starting directory
for root, dirs, files in os.walk(workspace, topdown=True): # starting traversing directories
for file in files: # loop through file names
if file.endswith('.mxd'): # if it's an mxd
mxd = arcpy.mapping.MapDocument(os.path.join(root,file))
els = arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT')
for el in els:
if 'old text' in el.text:
el.text = el.text.replace('old text','new text')
I suspect your problem is that your text box text doesn't equal '2016' but it contains '2016'.
Thanks for the reply. I'm a little confused on part of your script:
mxd = arcpy.mapping.MapDocument(os.path.join(root,file)) <-- What exactly are the parameters you used for the MapDocument function here? Because looking in the ArcPy Help, the parameters in that function appear as such:
Using your template, here is what I have so far:
May not be the problem, but your path syntax is not necessarily correct - use single forward slash, double backslash, or raw string notation (as you did for the first path).
Other than that, does the folder exist that you're trying to save to? And, obviously, do you have write access?
This time I ran it using the same path syntax in all instances. It ran and copied over the 1st_District_B.mxd, but that was all it did. Were you saying you used this script to work on all the mxds in a folder/drive? That's what i'm ultimately trying to do here.