3

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
arcpy.mapping.ExportToPNG(mxd,r"C:\Users\Kakemphaton\Desktop\out.png",df,1024,1024*ar,96,True)

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.

2 Answers 2

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)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Dec 6, 2014 at 19:55
5
  • 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 Commented Dec 6, 2014 at 20:33
  • There was an extra double quote in the ExportToPNG call. Fixed. Commented Dec 6, 2014 at 20:36
  • Too bad, now, i have : Parsing error SyntaxError: EOL while scanning string literal (line 6) , thanks Commented Dec 6, 2014 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. Commented Dec 6, 2014 at 21:02
  • It Works with 2 \ : doesn't work Desktop\" but Desktop\\" works, thanks Commented Dec 6, 2014 at 22:14
0

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))
answered Dec 9, 2014 at 1:01
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.