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.
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">as</SPAN> ap
<SPAN class="keyword token">import</SPAN> os
docs <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
exportPath <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
imgRes <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
imgJPEG <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Boolean</SPAN>
imgPDF <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Boolean</SPAN>
overwrite <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Boolean</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">transmit</SPAN><SPAN class="punctuation token">(</SPAN>message<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
ap<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>message<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">exportGraphics</SPAN><SPAN class="punctuation token">(</SPAN>docs<SPAN class="punctuation token">,</SPAN> exportPath<SPAN class="punctuation token">,</SPAN> imgRes<SPAN class="punctuation token">,</SPAN> imgJPEG<SPAN class="punctuation token">,</SPAN> imgPDF<SPAN class="punctuation token">,</SPAN> overwrite<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>overwrite<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'true'</SPAN><SPAN class="punctuation token">:</SPAN>
ap<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">for</SPAN> doc <SPAN class="keyword token">in</SPAN> docs<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">';'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
mxdname <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>basename<SPAN class="punctuation token">(</SPAN>doc<SPAN class="punctuation token">)</SPAN>
mxdname <SPAN class="operator token">=</SPAN> mxdname<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'.mxd'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
mxd <SPAN class="operator token">=</SPAN> ap<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>doc<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>imgJPEG<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'true'</SPAN><SPAN class="punctuation token">:</SPAN>
jpg <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>exportPath<SPAN class="punctuation token">,</SPAN>mxdname<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.jpg'</SPAN>
ap<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ExportToJPEG<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> jpg<SPAN class="punctuation token">,</SPAN> resolution <SPAN class="operator token">=</SPAN> imgRes<SPAN class="punctuation token">)</SPAN>
transmit<SPAN class="punctuation token">(</SPAN>mxdname <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.jpg Has Exported!'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>imgPDF<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'true'</SPAN><SPAN class="punctuation token">:</SPAN>
pdf <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>exportPath<SPAN class="punctuation token">,</SPAN>mxdname<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.pdf'</SPAN>
ap<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ExportToPDF<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN>pdf<SPAN class="punctuation token">,</SPAN> resolution <SPAN class="operator token">=</SPAN> imgRes<SPAN class="punctuation token">)</SPAN>
transmit<SPAN class="punctuation token">(</SPAN>mxdname <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.pdf Has Exported!'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">del</SPAN> mxd
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
exportGraphics<SPAN class="punctuation token">(</SPAN>docs<SPAN class="punctuation token">,</SPAN> exportPath<SPAN class="punctuation token">,</SPAN> imgRes<SPAN class="punctuation token">,</SPAN> imgJPEG<SPAN class="punctuation token">,</SPAN> imgPDF<SPAN class="punctuation token">,</SPAN> overwrite<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks in advance!
Judson