I’m building a custom tool, where you input the city and state and the tool outputs an image file of a map with certain features buffered, labeled, etc. I’m running into problems on the last step, exporting to PNG. Here’s the relevant code:
#Set workspace
import arcpy
import arcpy.mapping
import os
arcpy.env.overwriteOutput = True
yourstate = arcpy.GetParameterAsText(0)
city = arcpy.GetParameterAsText(1)
distances = arcpy.GetParameterAsText(2)
folderinput = arcpy.GetParameterAsText(3)
folderinput2 = '"'+ folderinput + '\"'
arcpy.env.workspace = folderinput2
(I deleted a bunch of code here…it’s for editing the mxd based on what state/city you select. FYI the GetParameterAsText(3) input is something like
C:\Users\first.last\Desktop\ArcGis\tool)
#exportasapng
mxd = arcpy.mapping.MapDocument("CURRENT")
fullname = '"' + city + yourstate + '.png' + '"'
arcpy.mapping.ExportToPNG(mxd, fullname, resolution=200)
Any suggestions? Here is the error when I try to run the tool:
Traceback (most recent call last):
File "C:\Users\first.last\Desktop\ArcGis\tool\toolv6.py", line 79, in <module>
arcpy.mapping.ExportToPNG(mxd, fullname, resolution=200)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\mapping.py", line 1228, in ExportToPNG
layout.exportToPNG(*args)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\mixins.py", line 586, in exportToPNG
return self._arc_object.exportToPNG(*args)
AttributeError: PageLayoutObject: Error in executing ExportToPNG
If I replace
arcpy.mapping.ExportToPNG(mxd, fullname, resolution=200)
with something like the following the tool exports successfully:
arcpy.mapping.ExportToPNG(mxd, "BuffaloNY.png", resolution=200)
….which is frustrating since “BuffaloNY.png” is what is being passed through as the parameter (it’s the result for print = fullname).
I’ve tried lots of minor changes, making sure the arcpy.mapping.ExportToPNG parameter is being passed as a literal string, changing the direction of the slashes, putting in a mxd.save before exporting, inserting the full pathname instead of relying on setting the environment, etc. and nothing is working. From searching around online, it seems like this is a problem other people have run into, but the solution wasn’t readily apparent.
Best Answer
I think I found my own solution, which mostly involved the formatting the parameters.
which seems to work. Thanks!