AnsweredAssumed Answered

Change Shapefile in a MXD with Python

Question asked by rfoscarini on Oct 8, 2015
Latest reply on Oct 8, 2015 by rfoscarini

Hi, i have a simple problem and i think there is a simple solution, but i can find or create it.

 

I have a lot of MXD (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 is the same, nothing changes, but the boundary.

 

Every time we open all the mxd 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 table of contents have a word with acccent (Área de Estudo) and the script doesnt recognize it. I translated all the script to all of you understand, but i'm from Brazil and we have a lot of words with accent, and the arcpy doesnt 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:\VALE SISTEMA SUL\PDE  TREVO\Geoprocessamento\1_Shapefile\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()

 

Message was edited by: Dan Patterson I have taken the liberty to format the post using Syntax highlighting found in the >> option Hope everything is as you posted.

Outcomes