I have a simple code, that changes a picture in the layout, based on the element name. It works fine, for all MXDs in the same folder of the script.
The problem is, the script saves every single .mxd file int the folder, not just the ones it actually changed the picture i wanted.
How can i make it to save just the ones it actually changes?
import arcpy, os inputPath = os.curdir #Loop through each MXD file for filename in os.listdir(inputPath): fullpath = os.path.join(inputPath, filename) if os.path.isfile(fullpath): if filename.lower().endswith(".mxd"): mxd = arcpy.mapping.MapDocument(fullpath) for elm in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT"): #find the element, and change it if elm.name == "logo": elm.sourceImage = r"C:\logo\newlogo.jpg" print fullpath mxd.save() del mxd
You just need to change your indents to only save if the element is changed.
#find the element, and change it if elm.name == "logo": elm.sourceImage = r"C:\logo\newlogo.jpg" print fullpath mxd.save()