0

I am new in ArcPy. i want to export month wise map with different title using ArcPy. Maps are exporting correctly but the title remains "January 2018". I used the following code. What is wrong?

import arcpy

mxd=arcpy.mapping.MapDocument("CURRENT")

df=arcpy.mapping.ListDataFrames(mxd,"Layers")

lyr=arcpy.mapping.ListLayers(mxd,"*2018")

month=["January 2018","February 2018","March 2018","April 2018","May 2018","June 2018"]

elm=arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","Date")

for el in elm:

  for ly in lyr:
    ly.visible= True
    arcpy.RefreshTOC()
    el.text=month[0]
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToJPEG(mxd,"C:/HILAL/Pamba GW Data/delete/Map/"+ly.name)
    ly.visible= False
    arcpy.RefreshTOC()
  • 1
    el.text=month[0] is always january (the first element in month list).you should loop over months. – umbe1987 Sep 7 '19 at 11:54
  • Remember that the software will only do what you tell it to do, not what you want it to do. el.text=month[0] needs to change. You haven't attempted to change the zero radix. – Vince Sep 7 '19 at 11:57
1

Why are you looping through elm if you want to change only one element which corresponds to the title? I assume elm is a list with only one element. Keeping this into account, you can remove your first for loop and you can change your current line for the following:

elm = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","Date")[0]

Furthermore, as @umbe1987 and @Vince pointed out, you are replacing the title everytime for the first element in month (i.e "January 2018"). Assuming lyr and month have the same number of elements (one layer for each month), you can use the enumerate function to loop through your layers and get a integer every loop corresponding to the month.

for i, ly in enumerate(lyr):
    ly.visible = True
    arcpy.RefreshTOC()
    elm.text = month[i]  # notice that el changed to elm
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToJPEG(mxd, "C:/HILAL/Pamba GW Data/delete/Map/" + ly.name)
    ly.visible = False
    arcpy.RefreshTOC()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.