[GIS] Problem getting the arcpy mapping module to export a layout view map

arcgis-10.0arcpy

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)[0]
for r in arcpy.ListRasters():
    year = r.split('_')[1]
    mo = r.split('_')[2]

    # automatically add new layers to legend
    legend = arcpy.mapping.ListLayoutElements(map, 'LEGEND_ELEMENT')[0]
    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)[0]       # 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.'

Expected output:
enter image description here

Actual output:
exported .png

Text element printed:

printed text element

Best Answer

As per comment above, when you are running from outside the session, then you can not use 'CURRENT'. If you are using script from outside always make sure to checkout your licence for Arc. It'll save you some time in future :) Despite difference in scale your program runs as expected. You do export df which is defined as df = arcpy.mapping.ListDataFrames(map)[0], which is exactly DataFrame with out MapSurrounding element and other graphic, which make up Page Layout. Try "PAGE_LAYOUT" instead of df.

Related Question