I'm using the arcpy mapping module to create a series of images and am having trouble exporting a map document to PNG in layout view; I can only get the script to export the data frame view. I've seen this post on setting the activeView attribute of the MapDocument object, but it hasn't worked for me. It wouldn't matter to me which view was exported except that I'd like to have a legend to identify each unique image.
Even more maddening is that I've added different text elements to both the data frame view and the layout view so that I can verify which is being exported. Even with the
map.activeView = 'PAGE_LAYOUT', the exported image has the data frame view text element (see below). However, when I print the text of the element using
print text_elm.text, it properly prints the layout view text.
Can anyone see anything wrong with my code? Thanks!
import os,arcpy imagesDir = r'--\images' gridsDir = r'--\grids' mxd = r'--\map\template.mxd' img_height_px = 800 img_width_px = 600 img_res_dpi = 300 arcpy.env.workspace = gridsDir # add difference raster to mxd and export as png map = arcpy.mapping.MapDocument( mxd ) map.activeView = 'PAGE_LAYOUT' #arcpy.RefreshActiveView() df = arcpy.mapping.ListDataFrames(map) for r in arcpy.ListRasters(): year = r.split('_') mo = r.split('_') # automatically add new layers to legend legend = arcpy.mapping.ListLayoutElements(map, 'LEGEND_ELEMENT') legend.autoAdd = True # newly added layers will be automatically added to the legend legend.elementHeight = 4 legend.elementWidth = 4 # add layer print 'Exporting layer:', os.path.join( imagesDir,'dif-%s-%s.png' % (year,mo) ) new_lyr = arcpy.mapping.Layer( os.path.join(gridsDir,r) ) arcpy.mapping.AddLayer(df, new_lyr, 'BOTTOM') # update symbology of new layer using source layer in the map temp_lyr = arcpy.mapping.ListLayers(map,'temp_lyr',df) # find template layer temp_lyr.visible = False # hide template layer arcpy.mapping.UpdateLayer(df,new_lyr,temp_lyr,True) # update symbology of new layer using template layer # export map layout view arcpy.mapping.ExportToPNG(map, os.path.join( imagesDir,'dif-%s-%s.png' % (year,mo) ),df ) del map print 'operation complete.'
Text element printed: