Search code examples
pythonarcgisarcpy

How to change part of the pathes to layers in a MXD-project


How to change part of the pathes to layers in a MXD-project (ArcMap) and save changes in a new project?


Solution

  • import arcpy, os
    
    def changeMXDFile(fullPath):
        nameWithPath, extension = os.path.splitext(fullPath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            mxd.findAndReplaceWorkspacePaths(u"\\10.64.68.36\j\folder\", u"\\10.64.68.36\j\f\folder\")
            outputFile = os.path.join(outputFolder, os.path.basename(nameWithPath) + ".mxd")
            mxd.saveACopy(outputFile, "10.3")
            arcpy.AddMessage("file " + os.path.basename(nameWithPath) + ".mxd is change")
            del mxd
    
    if __name__ == '__main__':
        inputFolder = arcpy.GetParameterAsText(0) #folder with input files mxd 
        outputFolder = arcpy.GetParameterAsText(1) #folder with output files mxd
        for fileName in os.listdir(inputFolder):
            fullPath = os.path.join(inputFolder, fileName)
            if os.path.isfile(fullPath): changeMXDFile(fullPath)