rafaelr

Text Elements and Graphic Element don´t show updates

Discussion created by rafaelr on Sep 6, 2011
Hi,

i just found out about the intriguing possibility to dynamically change layout elements with arcpy. unfortunately i can´t seem to get it working correctly.

i found this neat example on how to export different thematic layer to different pages in a pdf and would like to change a text element and it´s position based on the current layer´s name.
below is the edited example script.

import os, arcpy


#Specify output path and final output PDF
outPath = r"e:\test\info\\"
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + "ParcelReport.pdf")

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r"e:\test\testspi.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# specifiy the layout elements i want to change
textElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "textfield")[0]
titleBox = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "boxA4h")[0]


#Turn on visibility for each theme and export the page
lyrList = ["spJ_sappi", "sappi", "sappi_5klassen"]
for lyrName in lyrList:
    lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True
    
    
    # change the textbox´s text to the current layername and it´s position to be centered within the box
    textElem.text = str(lyrName)
    textElem.elementPositionX = ((titleBox.elementWidth - textElem.elementWidth)/2)+titleBox.elementPositionX
    print lyrName," ",textElem.elementPositionX
    arcpy.RefreshActiveView()
    
    
    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = outPath + lyrName + "_temp.pdf"
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    finalPdf.appendPages(tmpPdf)
    
    #Turn off layer visibility and clean up for next pass through the loop
    lyr.visible = False
    del lyr, tmpPdf
del mxd, df, finalPdf


changing the text of the box works fine, but the position won´t (i´ve also tried to save the mxd after each pass), as long as i run this outside of arcmap.
the only way to get it to work is by running it inside arcmap and changing the mxd to "CURRENT".

edit:
oh man do i feel stupid now...it´s textElem.elementPositionX not textElem.PositionX

Outcomes