This should solve the issue,import arcpy #set new SDE connection newSdePath = r"Database Connections\myConnection.sde" #get mxds as input from user mxdAr = arcpy.GetParameterAsText(0).split(";") #parse through the list of mxds, dataframes, layers; change data sources for mxdItem in mxdAr: mxd = arcpy.mapping.MapDocument(mxdItem.strip("'")) for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, ""): if lyr.isFeatureLayer== True: lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False) arcpy.RefreshTOC() arcpy.RefreshActiveView() mxd.save() del mxdWhen you add the script as a tool, in its properties, the user input is set as Map Document and Multivalue.
import arcpy #set new SDE connection newSdePath = r"Database Connections\myConnection.sde" #get mxds as input from user mxdAr = arcpy.GetParameterAsText(0).split(";") #parse through the list of mxds, dataframes, layers; change data sources for mxdItem in mxdAr: mxd = arcpy.mapping.MapDocument(mxdItem.strip("'")) for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, ""): if lyr.isFeatureLayer== True: lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False) arcpy.RefreshTOC() arcpy.RefreshActiveView() mxd.save() del mxd
Hi Jerome,You could use the glob module to do this. Here is an example:import arcpy, glob newSdePath = r"Database Connections\myConnection.sde" for file in glob.glob(arcpy.GetParameterAsText(0) + r"\*.mxd"): mxd = arcpy.mapping.MapDocument(file) for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, ""): if lyr.isFeatureLayer== True: lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False) mxd.save() del mxdIn the above example, set the data type for 'arcpy.GetParameterAsText' as 'Folder'. The above code will then loop through the specified folder and update all MXDs.
import arcpy, glob newSdePath = r"Database Connections\myConnection.sde" for file in glob.glob(arcpy.GetParameterAsText(0) + r"\*.mxd"): mxd = arcpy.mapping.MapDocument(file) for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, ""): if lyr.isFeatureLayer== True: lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False) mxd.save() del mxd
Hello, everybody!I have adapted a script that changes the data sources of the layers in one mxd provided by user input. All I want is to have this script accept multiple mxds from user and loop through the script.I know this situations has been described before, however I was not able to implement the advice found here.I am at the very beginning of my python experience, therefore any help on this would be much, much appreciated.I paste in the code with my trial for multiple mxd input:import arcpy#set new SDE connectionnewSdePath = r"Database Connections\myConnection.sde"#get mxds as input from usermxdAr = arcpy.GetParameterAsText(0)mxd = arcpy.mapping.MapDocument(mxdAr)#parse through the list of mxds, dataframes, layers; change data sourcesfor mxdItem in mxd.split(";"): for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, ""): if lyr.isFeatureLayer== True: lyr.replaceDataSource(newSdePath, "SDE_WORKSPACE","", False)arcpy.RefreshTOC()arcpy.RefreshActiveView()mxd.save()del mxdThe error i get :File "D:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 608, in __init__ assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")AssertionError: Invalid MXD filename.Thank you for your help!
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.