2

I work with multi MXD, and the title in all of them start with "The plan on the" and then i write the map's subject, for example:

"The plan on the land use", etc. I need to change just the sentence from "The plan on the" into "The blueprint on the". All other words in the title will remain the same. I try using this code:

import arcpy
from arcpy import env 
env.workspace = r"D:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
 print mxdname
 mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxdname)
 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
 if elm.text == 'The plan on the*': 
 elm.text = 'The blueprint on the*'
 print 'elm.text'
 mxd.save() 
del mxd

but it only print the MXD name:

>>> 
airplane noise.mxd
airplane noise2 a3.mxd
antiquities.mxd
conflicts.mxd
project.mxd
>>> 
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 10, 2016 at 14:22
1
  • Is the indent on the if elm.text... line correct? Commented Feb 10, 2016 at 14:42

2 Answers 2

7

Try this:

import arcpy
from arcpy import env 
env.workspace = r"D:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
 print mxdname
 mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxdname)
 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
 #fixed indent and replaced equality test.
 elm.text = elm.text.replace('The plan on the','The blueprint on the') 
 print elm.text
 mxd.save() 
del mxd
answered Feb 10, 2016 at 15:39
2
  • 1
    I think print 'elm.text' should be print elm.text Commented Feb 10, 2016 at 18:47
  • what do you mean by fixed indent and replaced equality test. Commented Aug 29, 2016 at 7:36
2

I see 3 things that need to be changed:

  1. For and if bodies both have to be indented. Here is a link: https://docs.python.org/2/tutorial/controlflow.html
  2. Wildcards don't work with equals =. You will need to manipulate strings in python. Here is a link: https://docs.python.org/2/library/stdtypes.html#string-methods
  3. When you print a variable, it should not be in quotes. Quotes serve to show the beginning and ending of a string. Variables may contain a string, but they are not a string.

Here is one way to do it.

import arcpy
from arcpy import env 
env.workspace = r"D:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
 print mxdname
 mxd = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxdname)
 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
 if elm.text.startswith('The plan on the'): 
 elm.text = elm.text.replace('The plan on the', 'The blueprint on the')
 print elm.text
 mxd.save() 
del mxd
answered Feb 10, 2016 at 18:37
2
  • 1
    RHB, how can i change just one word inside the sentence, for example i want to change just the word 'plan' into 'blueprint?' Commented Feb 11, 2016 at 7:36
  • @newGIS - open the link I gave you on string methods and find the one that is something like "contains" and use that instead of "startswith". "The replace part would be the same except with the appropriate strings. Commented Feb 11, 2016 at 17:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.