Batch Export And Print Script/Tool stopped working in 10.2

5180
8
12-06-2013 07:09 AM
YaroslavZaitsev
New Contributor III
Hi all. I have been using below script/tool for a very long time and have gotten used to how easy it was for me to take 100+ maps, click 2 things and come back to beautifully exported or printed dataset. My tool has stopped immediately after we upgraded to 10.2 from 10.0. I am sure it is something as silly as missing a squigly line somewhere. It seems that every release of arcmap is followed by some minor change. Initially with VB to Py I had to source all scripts to make them work and now my main stopped working. Any thoughts/comments would be appreciated. Thank you.

"""
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()

Tags (2)
0 Kudos
8 Replies
JeffBarrette
Esri Regular Contributor
How has it stopped working?

Do you have a line number or error message to help narrow down the issue?

Jeff
0 Kudos
YaroslavZaitsev
New Contributor III
It just stopped working. It goes into red X and halts the script and does not produce any error messages. But when I put the text into IDLE to run the tool it tells me that there is an error on line 15

Traceback (most recent call last):
  File "G:\share\arcgis\toolboxes\BatchExportandPrint.py", line 15, in <module>
    files= sys.argv[1]
IndexError: list index out of range

I have taken a few python classes in past couple of years. I think this error means that there are no variables... ok since this is a script and script came with a tool referring to the script. In tool you specify arguments (aka inputs)

In Arcmap itself it does this:

It accepts all the maps and runs the tool and then nothing happens. I see a red X saying tool did not run. A little failure window does not pop up and no error message shows up. Please see attached screenshot.
0 Kudos
MichaelNorelli
New Contributor II

I ran this successfully in ArcMap 10.2.1...

0 Kudos
MichaelNorelli
New Contributor II

...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

PDF    

JPG    

PNG    

EMF    

AI    

PRINT      

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! 

DanielleLee
New Contributor II

This worked perfectly for me! Thank you. 

0 Kudos
Seanmmm
New Contributor

Anyone have any luck with this in 10.5?? I'm getting the same thing as OP with his script, mysterious red X.

0 Kudos
RhettZufelt
MVP Frequent Contributor

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_

0 Kudos
MatejCunder
New Contributor II

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

0 Kudos