Arcpy Batch ExportToJPEG & OverwriteOutput Not Working

818
4
04-26-2017 09:19 AM
JudsonCrouch
New Contributor

Hello All,

I am working on a script to batch export a list of user selected MXD's to either JPEG, PDF, or JPEG & PDF using arcpy.mapping.exportToJPEG. I give the user the option to overwrite existing images in the selected output location however the JPEG's will not overwrite while the PDF's will. 

Does anyone know why this might happen? I have researched it a bit and am unable to find a solution.

import arcpy as ap
import os

docs = ap.GetParameterAsText(0)
exportPath = ap.GetParameterAsText(1)
imgRes = ap.GetParameterAsText(2)
imgJPEG = ap.GetParameterAsText(3)     #Boolean
imgPDF = ap.GetParameterAsText(4)     #Boolean
overwrite = ap.GetParameterAsText(5)     #Boolean

def transmit(message):
     ap.AddMessage(message)
     return

def exportGraphics(docs, exportPath, imgRes, imgJPEG, imgPDF, overwrite):
     
     if str(overwrite) == 'true':
          ap.env.overwriteOutput = True

     for doc in docs.split(';'):
          mxdname = os.path.basename(doc)
          mxdname = mxdname.split('.mxd')[0]

          mxd = ap.mapping.MapDocument(doc)

          if str(imgJPEG) == 'true':
               jpg = os.path.join(exportPath,mxdname) + '.jpg'
               ap.mapping.ExportToJPEG(mxd, jpg, resolution = imgRes)
               transmit(mxdname + '.jpg Has Exported!')
          if str(imgPDF) == 'true':
               pdf = os.path.join(exportPath,mxdname) + '.pdf'
               ap.mapping.ExportToPDF(mxd,pdf, resolution = imgRes)
               transmit(mxdname + '.pdf Has Exported!')

          del mxd



if __name__ == '__main__':

     exportGraphics(docs, exportPath, imgRes, imgJPEG, imgPDF, overwrite)

Thanks in advance!

Judson

0 Kudos
4 Replies
MitchHolley1
MVP Regular Contributor

What's the error you get?

0 Kudos
JudsonCrouch
New Contributor

It does not return any error when I run the script. It just does not overwrite the JPEG files but while overwrite the PDF files when exporting.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I don't know what 'overwrite' is returning in terms of string format, but if you are going to test strings, you need to check the possibility that the case of the string is incorrect

test = 'True'  # or 'TRUE', etcetera

str(test) == 'true'
False

str(test).lower() == 'true'
True
0 Kudos
JudsonCrouch
New Contributor

The str(overwrite) == 'true' returns a checkbox being checked in the script interface. The boolean parameter returns 'true' in lower case when the box is checked. 

0 Kudos