""" Batch Export and Print This script takes MXD files and exports all the maps as selected @author Steven Porter @contact: porters1@ohio.edu @organization: The Voinovich School for Leadership and Public Affairs, Ohio University @version 1/20/11 """ import arcpy, sys, os #Input variables. Remember arcgis passes lists as semicolon delimited strings (files) files= sys.argv[1] yesPDF = sys.argv[2] yesJPG = sys.argv[3] yesPNG = sys.argv[4] yesEMF = sys.argv[5] yesAI = sys.argv[6] yesPRINT = sys.argv[7] printer = sys.argv[8] #relies on code to generate a list in the tool itself outputFolder = sys.argv[9] mergePDF = sys.argv[10] #if outputFolder is empty use mxd location if outputFolder=="#": outputFolder = os.path.dirname(files.split(";")[0]) if yesPDF=="true" and mergePDF =="true": combinedpdf = arcpy.mapping.PDFDocumentCreate(outputFolder + os.sep + "CombinedMaps.pdf") #loop through each passed mxd file split the string back into the list for file in files.split(";"): file=file.strip("'") #parse out the file name of the file filename = os.path.basename(file) #get the name of file without any file extension mxd_name = filename.split(".")[0] #create a map object from the file map = arcpy.mapping map_document = map.MapDocument(file) #Check for broken data sources brokenList = arcpy.mapping.ListBrokenDataSources(map_document) if brokenList: errorString = filename+" has broken data sources on layer(s): " for item in brokenList: errorString = errorString + "'"+item.name+"'" arcpy.AddError(errorString) # Set all the parameters as variables here: data_frame = 'PAGE_LAYOUT' resolution = "300" image_quality = "NORMAL" colorspace = "RGB" compress_vectors = "True" image_compression = "DEFLATE" picture_symbol = 'RASTERIZE_BITMAP' convert_markers = "TRUE" embed_fonts = "True" layers_attributes = "NONE" georef_info = "False" #perform selected operations if yesPDF=="true": arcpy.AddMessage("Exporting: "+ filename+" as PDF") out_pdf = outputFolder + os.sep + mxd_name+ ".pdf" map.ExportToPDF(map_document, out_pdf, data_frame, 640, 480, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info) if mergePDF =="true": combinedpdf.appendPages(out_pdf) if yesJPG=="true": arcpy.AddMessage("Exporting: "+ filename+" as JPEG") out_jpg = outputFolder + os.sep + mxd_name+ ".jpg" map.ExportToJPEG(map_document, out_jpg) if yesPNG=="true": arcpy.AddMessage("Exporting: "+ filename+" as PNG") out_png = outputFolder + os.sep + mxd_name+ ".png" map.ExportToPNG(map_document, out_png, data_frame, 640, 480, resolution) if yesEMF=="true": arcpy.AddMessage("Exporting: "+ filename+" as EMF") out_emf= outputFolder + os.sep + mxd_name+ ".emf" map.ExportToEMF(map_document, out_emf, data_frame, 640, 480, resolution, image_quality, "#", picture_symbol, convert_markers) if yesAI=="true": arcpy.AddMessage("Exporting: "+ filename+" as AI") out_AI= outputFolder + os.sep + mxd_name+ ".ai" map.ExportToAI(map_document, out_AI) if yesPRINT=="true": arcpy.AddMessage("Printing: "+filename) map.PrintMap(map_document, printer) del map #delete the map opject if yesPDF=="true" and mergePDF =="true": combinedpdf.saveAndClose() del combinedpdf # This gives feedback in the script tool dialog arcpy.GetMessages()
I ran this successfully in ArcMap 10.2.1...
...by doing this:
I downloaded the whole toolbox and bin directory from here: snowballsteve/ArcGIS_Scripts · GitHub
Under the toolboxes directory: C:\Users\<user>\AppData\Roaming\ESRI\Desktop10.2\ArcToolbox
I stored the bin directory containing the python script above in ..\My Toolboxes\bin. The toolbox this script is called from, ArcGIS_Scripts.tbx, I stored in ..\MyToolboxes.
In ArcMap, I open Catalog > Toolboxes > My Toolboxes > ArcGIS_Scripts.tbx, and underneath is "Batch Export and Print".
Right-clicking and choosing Properties, I can see that the script above is listed under the Source tab as the Script File.
In the Parameters tab, I can see these:
Input MXD Documents
JPG
PNG
EMF
AI
Printer
Output Folder
Merge PDF
Double-clicking the tool runs it. This pops up the dialog where I specify my directory of MXDs and the checkboxes to say what I want done with them.
Runs with no errors and nice outputs!
This worked perfectly for me! Thank you.
Anyone have any luck with this in 10.5?? I'm getting the same thing as OP with his script, mysterious red X.
How are you running this tool? It appears as if it is not getting the sys.argv[1] passes to the script.
If a script tool, might want to check the properties and make sure all the parameter info is correct.
R_
Hi,
I was struggling with the same problem; the code just stopped working.
I finally found a solution, that wokrs for me.
In the scipt properties check the options: "Always run in foreground"
BR,
Matej