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?
Solved! Go to Solution.
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
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
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.