Change text using arcpy

4915
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
1 Solution

Accepted Solutions
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

View solution in original post

10 Replies
DanPatterson_Retired
MVP Emeritus

oldText = 'land use'  + '\n' + 'fuel' 

myString = 'free fuel + '\n'  + 'gas'

show some output

it isn't a python issue

Yaron_YosefCohen
Occasional Contributor II

i get:

>>>

environment 1.mxd

environment 2.mxd

environment 3.mxd

til i get

environment 25.mxd

>>>

it only print mxd name

0 Kudos
MichaelVolz
Esteemed Contributor

Print out the text elements without an if statement to ensure that your syntax for oldText is correct

0 Kudos
DanPatterson_Retired
MVP Emeritus

do you actually print out oldtext?  all you print out is

print mxdname 

ergo you get

environment 1.mxd

environment 2.mxd

environment 3.mxd

til i get

environment 25.mxd

>>>

it only print mxd name

put in print statements if you want to test stuff

0 Kudos
LukeWebb
Occasional Contributor III

Basic python question here!, you just get the mxd names, as the if statement is never satisfied.

To resolve, try changing your code as follows, then you should be able to work out what has happened!

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'  
    print "We are looking for a value of: "
    print oldText
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):  
        if elm.text == oldText:  
            print elm.name  
            elm.text = myString
        else:
            print "Non Matching Element value of:"
            print elm.text
    mxd.save()  
    counter = counter + 1  
del mxd  
Yaron_YosefCohen
Occasional Contributor II

i try your code Luke and got result :

>>> 
environment 1.mxd
We are looking for a value of: 
land use
fuel
Non Matching Element value of:
land use
fuel
environment 2.mxd
We are looking for a value of: 
land use
fuel
>>> 

the oldText still remain the same in the layout :

1.jpg

0 Kudos
DanPatterson_Retired
MVP Emeritus

there is a couple of possibilities:

  • the words 'land' or 'use' might containing leading or trailing spaces
    • try "land " + "\nl" + "use"
    • or "land" + "\nl" + " use"
    • etc
  • what appears as one label stacked one over the other is actually two labels... one called land and the other called use  To confirm...try clicking on the label in the map and see if one or both words get highlighted
0 Kudos
DanPatterson_Retired
MVP Emeritus

To elaborate consider the following examples (not all combinations checked):

>>> a = "land" + "\nl" + "use"
>>> a == a
True
>>> # now try some variations
>>> b = "land " + "\nl" + "use"  # extra space after 'land'
>>> a == b
False
>>> c = "land" + "\nl" + "use "  # extra space after 'use'
>>> a == c
False
>>> d = "land" + "use"
>>> a == d
False
Yaron_YosefCohen
Occasional Contributor II

i will try-thanks

0 Kudos