Deleting Layout Elements with arcpy.mapping

5290
9
03-29-2011 07:42 AM
MichaelSiadak
New Contributor III
Is there a way to delete layout elements through arcpy.mapping?  I'm not seeing one in the 10 docs.

I presume that a workaround would be to just change the X or Y position of the element to outside the bounds of the page (although I haven't tried it), but there should really be a clean way to delete a layout element with an arcpy.mapping script.

Any insight, ESRI employees/arcpy experts?
9 Replies
DanPatterson_Retired
MVP Emeritus
I suspect it depends upon the element that you wish to change, for example, the sample code in the help file found here:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000000m000000.htm

contains lines of code...

    if elm.text == "2009":
        elm.text = "2010"


which suggests that you could simply change it to something like this

    if elm.text == "2009":
        elm.text = ""


essentially "removing" it
0 Kudos
JeffBarrette
Esri Regular Contributor
You can not create or remove layout elements using arcpy.mapping.

You can either move elements on and off the page or like Dan suggests, in the case of text elements you can set the string to be a blank space.

NOTE - don't set the string to be completely empty (e.g., "").  This causes issues that are being addressed at SP2.  The UI doesn't let you do it either.  Set the string to be a single blank (e.g., " ").

Jeff
MichaelSiadak
New Contributor III
Thanks for the responses everyone.  I can confirm that moving an element off the page does work, as does Dan's suggestion of zeroing out the text in a text element.

Jeff, is there some kind of design reason that I'm missing for why you can't create or delete layout elements directly via arcpy.mapping?  This is the exact kind of one-off map composition functionality that makes arcpy.mapping useful, but it's missing. I can't imagine that it would be that difficult to implement (especially deleting), just a few more ArcObjects routines exposed to the Python api...
0 Kudos
JeffBarrette
Esri Regular Contributor
It is a great question but of course the answer isn't that simple.  Here is a very quick summary:

Our objective with arcpy.mapping was NOT to recreate ArcObjects capabilities but to simplify the scripting experience.  If we gave you the ability to add new objects (e.g., scale bar) we would need to expose ALL of the properties associated with adding that object (aka ArcObjects).

Arcpy.mapping is intended to work with already authored mxds and layer files.  The idea is that we use ArcMap and all of its 100's of buttons, menus, properties dialogs to author the content and we use arcpy.mapping to automate it.

It is difficult for us to draw a line in the sand and are always looking for good feedback on when/how we should expand the API to further enhance map automation (not map creation).

At 10.1 we are going to expand the symbology API because our users showed us some really nice use cases.

Please, send us your feedback.
Jeff
0 Kudos
MartinRick
Occasional Contributor II
Hi Jeff,

I had the same problem like Michael and would like to create resp. delete Layout elements using Python. At the same time I can completely understand that it is not a small issue for ESRI to give more power to the Python API. Still it would be great if Esri could enhance the Pyhton capabilities because it is easier to program than e.g. ArcObjects for Java.
I am a big fan of the Python API esp. because of the great documentation with all the example codes! You guys at Esri do a really good job with it and should go on with it.

Regards,

Martin
0 Kudos
JamesCrandall
MVP Frequent Contributor
You can not create or remove layout elements using arcpy.mapping.

Jeff


Okay.  So then just how are we supposed to create new layout elements?

I need to create a new Text Element in a layout view where the user clicks.  This needs to be done in a Python Add-In.
0 Kudos
JeffBarrette
Esri Regular Contributor
At 10.1 we added graphic element and text element cloning.  If you have a pre-authored text element in your layout, then you can clone it an place it as many times as you need.

Jeff
0 Kudos
JamesCrandall
MVP Frequent Contributor
At 10.1 we added graphic element and text element cloning.  If you have a pre-authored text element in your layout, then you can clone it an place it as many times as you need.

Jeff


Jeff -- I think I understand this now.  From my other thread in the Python forum, I was under the impression that cloned elements could be added to different/new .mxd documents.  From what you mention above, this sounds like it is not possible with the arcpy.mapping module and this functionality is limited to within a single .mxd and with elements that already exist.

That is, I cannot add new elements to any .mxd and I cannot add cloned elements from one .mxd to another.

Thanks,
james
0 Kudos
JeffBarrette
Esri Regular Contributor
James, you are correct.  Graphic or text elements can only be cloned within a single map document.

Jeff
0 Kudos