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
>>>
asked Feb 10, 2016 at 14:22
2 Answers 2
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
-
1I think
print 'elm.text'
should beprint elm.text
2016年02月10日 18:47:18 +00:00Commented Feb 10, 2016 at 18:47 -
what do you mean by
fixed indent and replaced equality test.
GIS Data Butcher– GIS Data Butcher2016年08月29日 07:36:54 +00:00Commented Aug 29, 2016 at 7:36
I see 3 things that need to be changed:
- For and if bodies both have to be indented. Here is a link: https://docs.python.org/2/tutorial/controlflow.html
- 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
- 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
-
1RHB, how can i change just one word inside the sentence, for example i want to change just the word 'plan' into 'blueprint?'newGIS– newGIS2016年02月11日 07:36:24 +00:00Commented 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.RHB– RHB2016年02月11日 17:15:11 +00:00Commented Feb 11, 2016 at 17:15
lang-py
if elm.text...
line correct?