Problems changing value of a text element in Esri Arcmap

201 views Asked by At

I have a mxd file to print a report, it has a map and text elements, I'm trying to change a value of text element but I'm not able to do it. this is the code:

import arcpy
import os
import datetime

def Actualiza_Texto(nombre, valor):
   elementos = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
   for elem in elementos:
      if elem.name == nombre:
         if type(valor) == str:
            elem.text = unicode(valor, "utf-8", errors="ignore")
         else:
            elem.text = unicode(valor)

Web_Map_as_JSON = arcpy.GetParameterAsText(0) 
tRazonSocial = arcpy.GetParameterAsText(1)

ahora = datetime.datetime.now()
output = 'Calculo_{}_{}_{}_{}_{}_{}.{}'.format(ahora.year,ahora.month,ahora.day, ahora.hour,ahora.minute,ahora.second, "PDF") 
templateMxd = os.path.join('c:/', "Carta_Vertical_1.mxd")
result = arcpy.mapping.ConvertWebMapToMapDocument(Web_Map_as_JSON, templateMxd)
mxd = result.mapDocument

Actualiza_Texto('tRazonSocial', tRazonSocial)
arcpy.AddMessage(tRazonSocial)
Output_File = os.path.join(arcpy.env.scratchFolder, output)
Actualiza_Texto('tDocumento', Output_File)
arcpy.mapping.ExportToPDF(mxd, Output_File) 
arcpy.AddMessage("Se entrega el resultado como parametro {}".format(Output_File))
arcpy.SetParameterAsText(2, Output_File)

filePath = mxd.filePath
del mxd, result
os.remove(filePath)
arcpy.AddMessage("***FIN***")

All parameter arrives well, but it doesn't change value of text element.

1

There are 1 answers

0
Octavio Cárdenas On BEST ANSWER

I've found the solution, I had to change "name" attribute by "text" in the for block and all work fine.