I have a lot of MXDs (more than 200) and all of them use a polygon shapefile "study_area_v00.shp". In the data frame, its name is just "Study Area".
Every time we change the boundary of the polygon, we save a new copy to "study_area_v01.shp" an so on. All the attributes in table are the same, nothing changes, but the boundary.
Every time we open all the mxds and change the data source, one by one because none of pythons I found, work fine.
This script works fine, and change all the layerfiles in all MXD in a folder. But there is a little detail in my case: my layer name in the table of contents have a word with acccent (Área de Estudo) and the script doesn't recognize it. I translated all the script for all of you to understand, but I'm from Brazil and we have a lot of words with accent, and the arcpy doesn't understand it.
Can you help me?
import arcpy, os
inputPath = os.curdir
outputPath = os.curdir
#layerfile name we want to change, in table of contents
layerfile = "Área de Estudo"
#Folder path of the new shapefile
FolderPath = r"T:\.......\Utm"
#Name of the new shapefile, in folder path (without '.shp')
NewShapefile = "AreasEstudo_PDETrevo_v00"
#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)
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
if lyr.name == layerfile:
print layerfile
lyr.replaceDataSource(FolderPath, "SHAPEFILE_WORKSPACE", NewShapefile)
print fullpath
mxd.save()
arcpy
should be able to leverage the info here as it is just a python lib. Hopefully that helps. Additionally here is a knowledge base article from ESRI that deals with the topic. Here is a related question on GIS Stack Exchange.