How to save just some mxds through 'if' in python?

by raphael   Last Updated May 10, 2017 20:22 PM

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


Answers 1


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()
Emil Brundage
Emil Brundage
May 10, 2017 19:33 PM

Related Questions


Change a script to run over subfolders

Updated May 11, 2017 13:22 PM