Select to view content in your preferred language

Problem with arcpy.mapping Title and Legend

2887
2
03-08-2011 06:32 AM
BernardoG_
Emerging Contributor
Hi Every body
I am new at python and I have problem adding a title and legend to the layout by arcpy.mapping.
I tried ESRI's samples which where too complicated (Specifically the Legend Example!) and I could not add the legend a simple title to map!
Could you please let me know how i can do this?
I also need to know how I can resize or position the data frame in map layout.

I used this code to create title which didn't add any thing
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Test\Map.mxd")
#search for ???Title Block??? and if found move to desired location
for elm in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT"):
if elm.name == "My Map":
elm.elementPositionX = 4.75
elm.elementPositionY = 10.5
mxd.save()
del mxd


Thanks
Tags (2)
0 Kudos
2 Replies
BernardoG_
Emerging Contributor
I also tried this code
mapTitleText = "Salam"
activeMXD = arcpy.mapping.MapDocument("CURRENT")
# Identify the map title element.
mapTitle = arcpy.mapping.ListLayoutElements(activeMXD, "TEXT_ELEMENT", "mapTitle")[0]

which I get the following error!

<type 'exceptions.IndexError'>: list index out of range
Failed to execute (Title).
Failed at Tue Mar 08 09:13:33 2011 (Elapsed Time: 0.00 seconds)

and when I run this code to create legend
import os
import sys
import arcpy
lyr  = "roads" 
mxd = arcpy.mapping.MapDocument(r"H:\\My Files\\Test\\Project")
df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]
lyr = arcpy.mapping.Layer(r"vanroads")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
legend.autoAdd = True
legend.adjustColumnCount(2)

I am getting this error
Running script Legend...
<type 'exceptions.AssertionError'>: Invalid MXD filename.
Failed to execute (Legend).
Failed at Tue Mar 08 09:45:23 2011 (Elapsed Time: 0.00 seconds)
0 Kudos
JasonScheirer
Esri Alum
Try this in the Python window:

print ", ".join(el.name for el in arcpy.mapping.ListLayoutElements(activeMXD))

this will list the names of every layout element in the document. It may be the case that the name is wrong in the script or it is not found because it is in a group element.

If you are using the raw flag on a string, you don't need to escape backslashes. Use one of:
mxd = arcpy.mapping.MapDocument("H:\\My Files\\Test\\Project.mxd")

or
mxd = arcpy.mapping.MapDocument(r"H:\My Files\Test\Project.mxd")


Again, ensure the element is being found:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Test\Map.mxd")
#search for �??Title Block�?� and if found move to desired location
for elm in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT"):
if elm.name == "My Map":
    print "FOUND IT!"
    elm.elementPositionX = 4.75
    elm.elementPositionY = 10.5
0 Kudos