Select to view content in your preferred language

Change Shapefile in a MXD with Python

3501
5
Jump to solution
10-08-2015 07:31 AM
Raphael_Augusto_FoscariniFerre
Regular Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
LukeSturtevant
Frequent Contributor

I just did a test run with the same layer name and I was able to successfully change the layer name using the following code:

for layer in arcpy.mapping.ListLayers(mxd,"",df):
     if layer.name == u'\xc1rea de Estudo':
         layer.name = "Area de Estudo"

That being said you could probably set your layerfile = '\xc1rea de Estudo' and your script may work.

View solution in original post

0 Kudos
5 Replies
WesMiller
Deactivated User

Try changing the data source path Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop  instead of looping through the data frame layers.

0 Kudos
LukeSturtevant
Frequent Contributor

I just did a test run with the same layer name and I was able to successfully change the layer name using the following code:

for layer in arcpy.mapping.ListLayers(mxd,"",df):
     if layer.name == u'\xc1rea de Estudo':
         layer.name = "Area de Estudo"

That being said you could probably set your layerfile = '\xc1rea de Estudo' and your script may work.

0 Kudos
Raphael_Augusto_FoscariniFerre
Regular Contributor

Thanks man! It worked fine!

Thanks a lot!

How can i know about every code that the python will consider my accents? This is just one case!

Is there a dictionay?

some examples:

á, é, í, ó, ú

ã õ

â ê

ç

0 Kudos
LukeSturtevant
Frequent Contributor

I'm not too familiar with the codecs so I'm sure someone else could be more informative, but I'm sure you could find something that would be useful at the following sites:

4.9.2 Standard Encodings

and the codecs python module:

7.8. codecs — Codec registry and base classes — Python 2.7.10 documentation

you can also use the ord() and unichr() functions. For example:

>>> unichr(ord('Á'))

u'\xc1'

>>> unichr(ord('ó'))

u'\xf3'

so to find 'Área de Estudo' with python you could concatenate:

>>> layerfile = unichr(ord('Á'))+'rea de Estudo'

u'\xc1rea de Estudo'

also I found that if you create a list of your accent characters and then return the items it prints how python sees them:

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

>>> charList

['\xe1', '\xe9', '\xed', '\xf3', '\xfa', '\xe3', '\xf5', '\xe2', '\xea', '\xe7']

So that might also be useful in some cases.

0 Kudos
Raphael_Augusto_FoscariniFerre
Regular Contributor

thanks a lot, again!!

0 Kudos