I need to run the python script below that changes layer datasources inside ArcMap when the mxd is open.
How do I run the script with ArcMap Open?
from arcpy import env
from arcpy import mapping
newDataSource = r"Database Connections\NewSDEConnection.sde"
mapdoc = arcpy.mapping.MapDocument("CURRENT")
#mapdoc.findAndReplaceWorkspacePaths("<the directory you are changing from>" , newDataSource)
for df in mapping.ListDataFrames(mapdoc, "*"):
for lyr in mapping.ListLayers(mapdoc, "*", df):
theData = lyr.dataSource
print "the Data " + theData
theWorkspacePath = lyr.workspacePath
if theData.find(".ImageServer") == -1:
if ".sde" in theWorkspacePath.lower():
except Exception as e:
print "The following error has occured:"
Code Formatting the basics... to rule some apparent indentation errors.
Did you open the python window in arcmap and try to run it from there?
Are you doing this one mxd at a time? If not, why not use ArcCatalog Set Data Sources tool?
Michael, it does not work with ArcCatalog Change Datasource because maps are using a Modling extension and when we tried to change the datasource in ArcMap it reset the model, and the user looses everything. So I am gdoing this one mxd at a time for some specific users.
Where is the Python window in ArcMap? Can I load a python script?
You should be able to use `execfile('path/to/script.py')` to run your script from the python window in ArcMap.