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
>>> 
  • Is the indent on the if elm.text... line correct? – kenbuja Feb 10 '16 at 14:42
6

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
  • 1
    I think print 'elm.text' should be print elm.text – Midavalo Feb 10 '16 at 18:47
  • what do you mean by fixed indent and replaced equality test. – GIS Data Butcher Aug 29 '16 at 7:36
1

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
  • RHB, how can i change just one word inside the sentence, for example i want to change just the word 'plan' into 'blueprint?' – newGIS Feb 11 '16 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. – RHB Feb 11 '16 at 17:15

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.