2

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()
  • 1
    Here is a link to the python documentation of how to deal with Unicode characters. 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. – GeoSharp Oct 8 '15 at 15:22
  • 2
    In another forum, i got the answer to my problem. Just change this: layerfile = "Área de Estudo" to layerfile = u'\xc1rea de Estudo' – raphael Oct 8 '15 at 16:39
  • 1
    I think you should write that up as an answer instead of just a comment. I could not find the same question being asked previously and I'm sure it will come up again. It is perfectly acceptable to answer your own question here. – PolyGeo Oct 9 '15 at 2:24
1

Thanks for the advice.

In another forum, i got the answer to my problem. I just changed this:

layerfile = "Área de Estudo" to layerfile = u'\xc1rea de Estudo'

Here is a way to know how python 'reads' accents:

charList = ['á','é','í','ó','ú','ã','õ','â','ê','ç']
print charList

it shows this:

('\xe1', '\xe9', '\xed', '\xf3', '\xfa', '\xe3', '\xf5', '\xe2', '\xea', '\xe7')

Now i wont have this problem anymore!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.