Select to view content in your preferred language

Delete text from multiple mxds - python

2985
2
Jump to solution
12-09-2015 10:26 AM
melsoares
New Contributor

I have multiple mxds containing the word "draft" in a text box.  I'm looking for a python script that will allow me to point to the mxd's home folder on our file server and delete the text box from each mxd file.  Has anyone created such a script?

0 Kudos
1 Solution

Accepted Solutions
LukeSturtevant
Occasional Contributor III

Does this text element have an element name?

I think you might be looking for something like this:

import arcpy
from arcpy import env

env.workspace = # Folder with mxds

for mxds in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(env.workspace, mxds)
    mxd = arcpy.mapping.MapDocument(filePath)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
          if elm.name == "Draft":
              elm.text = ""
    mxd.save()
    del mxd

There may be a way to actually delete the text box, but this code will at least replace the "Draft" text with an empty string, if the element name is called "Draft".

If you do not have an element name then you could use something like this

import arcpy
from arcpy import env

env.workspace = # Folder with mxds

for mxds in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(env.workspace, mxds)
    mxd = arcpy.mapping.MapDocument(filePath)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
          if elm.text == "Draft":
              elm.text = ""
    mxd.save()
    del mxd

View solution in original post

2 Replies
LukeSturtevant
Occasional Contributor III

Does this text element have an element name?

I think you might be looking for something like this:

import arcpy
from arcpy import env

env.workspace = # Folder with mxds

for mxds in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(env.workspace, mxds)
    mxd = arcpy.mapping.MapDocument(filePath)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
          if elm.name == "Draft":
              elm.text = ""
    mxd.save()
    del mxd

There may be a way to actually delete the text box, but this code will at least replace the "Draft" text with an empty string, if the element name is called "Draft".

If you do not have an element name then you could use something like this

import arcpy
from arcpy import env

env.workspace = # Folder with mxds

for mxds in arcpy.ListFiles("*.mxd"):
    filePath = os.path.join(env.workspace, mxds)
    mxd = arcpy.mapping.MapDocument(filePath)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
          if elm.text == "Draft":
              elm.text = ""
    mxd.save()
    del mxd
melsoares
New Contributor

Thanks for passing this on, Luke.  After a few tweaks, I got the second script to work (the text boxes did not have an element name).

Two things for other readers:

I had to change line 1 to read: import os, arcpy

I had to add a space between the quotation marks in line 11.