I am trying to loop through a bunch of mxds in a folder and make some changes, in this case in particular I am trying to replace a text string. This should change all instances of "9/26/2014" with the dynamic date tag. The code runs fine but when I open one of the mxds, the date is not updated. Is there some saving or refreshing that has to be done that i'm not doing currently?
import os
import sys
import string
folderPath = '[folder path goes here]'
sort_list=[]
oldText = "9/26/2014"
newText = "<dyn type='date' format='short'/>"
for filename_1 in os.listdir(folderPath):
sort_list.append(filename_1) #append to empty list
list_length = len(sort_list) #list length for progress
sort_list.sort() #sort
for filename in sort_list:
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath): #check if its a file
basename, extension = os.path.splitext(fullpath) #split into the name and extension
if extension.lower() == ".mxd": #if its an ArcGIS document
mxd = arcpy.mapping.MapDocument(fullpath)
print mxd
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
print df
#Find all page layout text elements
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if oldText in elm.text:
print(oldText)
elmText = elm.text.replace(oldText, newText)
elm.text = elmText
print(elmText)
arcpy.RefreshActiveView()
mxd.save
arcpy.RefreshActiveView()