Change text using arcpy

4901
10
Jump to solution
08-11-2015 03:53 AM
Yaron_YosefCohen
Occasional Contributor II

Hi  there,

i try to change oldText with myString in 25 mxd's ( in the layout ).

The text (i used draw tool to create it) contain 2 row:

land use

fuel

myString is the new text

i try this code but python only print list of mxd names in the folder:

import arcpy
from arcpy import env 

env.workspace = r"D:\PROJECTS\ab\gis"
counter = 0
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    oldText = 'land use'
    oldText = oldText + '\n'
    oldText = oldText + 'fuel'
    mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\ab\gis\\" + mxdname)
    myString = 'free fuel'
    myString = myString + '\n' 
    myString = myString + 'gas'
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == oldText :
            print elm.name
            elm.text = myString
    mxd.save()
    counter = counter + 1
del mxd

is it possible with python 2.7.8?

thanks in advance.

0 Kudos
10 Replies
Yaron_YosefCohen
Occasional Contributor II

thanks for everyone,

finally i use this code:

import arcpy
from arcpy import env 

env.workspace = r"D:\PROJECTS\ab\gis"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\ab\gis\\" + mxdname)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        oldText = 'land use\nfuel'
        newText = u'free fuel\ngas'
        if elm.text == oldText:
            elm.text = newText
            print 'elm.text changed'
     
    mxd.save() 
del mxd