I use this code to export a dataframe to png, and it works.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
ar = df.extent.height / df.extent.width

Code from: Writing world file when exporting TIFF using ArcPy Mapping?

This code exports only the first dataframe and always the first. I can't export others and I have multiple dataframes in my mxd (example_1, example_2, example_3). How could I export example_2 and example_3? I tried to activate it for example_2, but it always exports example_1.


List the dataframes, then iterate through them (untested):

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
dataframes = arcpy.mapping.ListDataFrames(mxd)
for df in dataframes:
    ar = df.extent.height / df.extent.width
    arcpy.mapping.ExportToPNG(mxd, r"C:\Users\Kakemphaton\Desktop\" + df.name + ".png",df, 1024, 1024*ar, 96, True)
  • Hello,Thanks, but i encounter a Runtime error Traceback (most recent call last): File "<string>", line 6, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 1210, in ExportToPNG layout.exportToPNG(*args) AttributeError: DataFrameObject: Error in executing ExportToPNG – John Ecnaligivoc Dec 6 '14 at 20:33
  • There was an extra double quote in the ExportToPNG call. Fixed. – Chad Cooper Dec 6 '14 at 20:36
  • Too bad, now, i have : Parsing error SyntaxError: EOL while scanning string literal (line 6) , thanks – John Ecnaligivoc Dec 6 '14 at 20:43
  • I think the last line being broken over two lines may have been confusing - try a copy/paste of the code again after the edit I just made to it. – PolyGeo Dec 6 '14 at 21:02
  • It Works with 2 \ : doesn't work Desktop\" but Desktop\\" works, thanks – John Ecnaligivoc Dec 6 '14 at 22:14

I like to encourage the use of better string formatting and better ways to join paths. By getting rid of all those plus signs, etc., it will be less likely to encounter such errors - here is a much cleaner approach and it's much nicer to read:

import os    
os.path.join(r"C:\Users\Kakemphaton\Desktop", "{0}.png".format(df.name))

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.