Select to view content in your preferred language

help with text element

602
5
03-31-2011 05:57 AM
FrankVignati
Occasional Contributor II
updating a text element in an mxd is not working for me
ArcGIS 10 - 10.1.2800
there is a text string element in the mxd "SHEET   3002" that i want to change based on a variable
this was tested on an mxd that only has this text element in it and nothing else

this is the code to change the text element it that is not working:

import arcpy
import arcpy.mapping
MXD = arcpy.mapping.MapDocument("CURRENT")
for SheetNum in arcpy.mapping.ListLayoutElements(MXD, "TEXT_ELEMENT", "SHEET*"):
    SheetNum.text = "SHEET   7001"


if the code is:
import arcpy
import arcpy.mapping
MXD = arcpy.mapping.MapDocument("CURRENT")
for SheetNum in arcpy.mapping.ListLayoutElements(MXD, "TEXT_ELEMENT", "SHEET*"):
   if SheetNum.type == "TEXT_ELEMENT":
       SheetNum.text = "SHEET   7001"

still nothing happens

if i try:
SheetNum = arcpy.mapping.ListLayoutElements(MXD, "TEXT_ELEMENT", "SHEET*")[0]

it returns:
Runtime error <type 'exceptions.IndexError'>: list index out of range

what is wrong with this?
Tags (2)
0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
You will need to add 'arcpy.RefreshActiveView()' at the end of your code.  Try the following:

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*SHEET*"):
    if elm.type == "TEXT_ELEMENT":
        elm.text = "SHEET 7001"

arcpy.RefreshActiveView()      


Note:  I also had to add an '*' in front of SHEET for the wildcard to work correctly.
0 Kudos
FrankVignati
Occasional Contributor II
You will need to add 'arcpy.RefreshActiveView()' at the end of your code.  Try the following:

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*SHEET*"):
    if elm.type == "TEXT_ELEMENT":
        elm.text = "SHEET 7001"

arcpy.RefreshActiveView()      


Note:  I also had to add an '*' in front of SHEET for the wildcard to work correctly.


if i add an asterisk before SHEET than every text element gets selected, please see picture

is there a way to put a Like operator for python?
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT", "*SHEET*"):
    if SheetNum.text LIKE "SHEET*"
        SheetNum.text = "SHEET <Some Number>"


edit:
i thought maybe the element could be captured by it's location, but this also returns nothing

>>> for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT", "*SHEET*"):
...     if SheetNum.text == "SHEET   2156":
...         print SheetNum.elementPositionX
...
38.926

>>> for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT", "*SHEET*"):
...     if SheetNum.elementPositionX == 38.926:
...         print SheetNum.text
...
>>> for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT", "*SHEET*"):
...     if SheetNum.elementPositionX == "38.926":
...         print SheetNum.text
0 Kudos
JakeSkinner
Esri Esteemed Contributor
This looks to be a bug with the ListLayoutElements function.  The "SHEET*" wildcard should be returning the text elements that begin with "SHEET".  I would recommend reporting this to tech support so they can document this bug.  I haven't been able to find a workaround yet, but I'll keep looking a little bit longer.
0 Kudos
FrankVignati
Occasional Contributor II
This looks to be a bug with the ListLayoutElements function.  The "SHEET*" wildcard should be returning the text elements that begin with "SHEET".  I would recommend reporting this to tech support so they can document this bug.  I haven't been able to find a workaround yet, but I'll keep looking a little bit longer.


thank you for your time, any help would be greatly appreciated
i tried adding it as a title intstead, is there a way to change a title in arcpy?
0 Kudos
FrankVignati
Occasional Contributor II
this is clunky but it works, the text element was set to SHEET  0000 on the mxd
then ran this code:


## Set the Sheet Number
Sheet = arcpy.GetParameterAsText(0) # 7001 for example

## Set the Sheet Number
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
   if SheetNum.text == "SHEET   0000":
       SheetNum.text = r"SHEET   %s"%(Sheet)

## Export the map to PDF
arcpy.mapping.ExportToPDF(IMXD, Output)

## Set the Sheet Number back
for SheetNum in arcpy.mapping.ListLayoutElements(IMXD, "TEXT_ELEMENT"):
   if SheetNum.text == r"SHEET   %s"%(Sheet):
       SheetNum.text = "SHEET   0000"
0 Kudos