Select to view content in your preferred language

Exporting Data Driven Pages to JPEG - modeling export location as parameter

6335
5
Jump to solution
02-17-2016 06:29 AM
JerryGarcia1
Deactivated User

Hello,

I am still new to Python and I am stuck on how to allow the user of the tool to set the location of the resulting JPEGs. The script I have so far is below

import arcpy,os,sys

import arcpy.mapping

import arcpy.utils

import arcpy.arcobjects

import_path = r"C:\GIS_DATA\Python_Scripts\python_jpeg_example.mxd"   # Path of .mxd

export_path = r"C:\GIS_DATA\Python_Scripts"   # Path of output file

field_name = "DIST_NAME" # Name of field used to sort DDP

mxd = arcpy.mapping.MapDocument(import_path)

for i in range(1, mxd.dataDrivenPages.pageCount + 1):

   mxd.dataDrivenPages.currentPageID = i

   row = mxd.dataDrivenPages.pageRow

   print row.getValue(field_name)

   print export_path + "." + row.getValue(field_name) + ".jpg"

   arcpy.mapping.ExportToJPEG(mxd, export_path + "." + row.getValue(field_name) + ".jpg")

del mxd

When adding the tool in I set the export path as a parameter (set to workspace). No mater where it is set it always drops it in c:\GIS_DATA.  I thought I could use GetParameterAsText but I want the user to navigate to the folder they want the JPEGs to reside instead of having to type in the location.

Any help would be greatly appreciated. 

0 Kudos
1 Solution

Accepted Solutions
PeterWilson
Frequent Contributor

Hi Jerry

The reason that its writing the results out to "C:\GIS_DATA" is that the export path has been set to that directory manually. What you want to do is set the input_mxd and output_folder to become arguments that you can supply as the user of the script.  You need to set your input_mxd and output_folder variables to arcpy.GetParameterAsText to change the input and output arguments for each time you run the script as a tool within ArcGIS. I've cleaned your code a bit and gave explanations why.

'''
Created on Feb 17, 2016

Export Data Driven Pages

to jpegs

@author: PeterW
'''
# I've removed the unnecessary modules that you imported

# import modules and site-packages
import os
import arcpy

# set input and output arguments
input_mxd = arcpy.GetParameterAsText(0)
output_folder = arcpy.GetParameterAsText(1)
field_name = r"DIST_NAME"

# export each data driven page out as a jpeg 
mxd = arcpy.mapping.MapDocument(input_mxd)
for i in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = i
    row = mxd.dataDrivenPages.pageRow
    # I've created a variable page_name to store the current page name
    page_name = row.getValue(field_name)
    arcpy.AddMessage("Processing {}".format(page_name))
    # I've used os.path.join to join the path and file name for the output jpegs
    arcpy.mapping.ExportToJPEG(mxd, os.path.join(output_folder,  page_name + ".jpg"))
del mxd

View solution in original post

5 Replies
AdrianWelsh
MVP Honored Contributor

I know the Python hot shots on this board will chime in with excellent answers. I have used the Tkinter module for opening a browse file dialog (well, really the tkFileDialog). Here is a brief example I found on Stack Exchange:

http://stackoverflow.com/questions/19944712/browse-for-file-path-in-python

This may work for you

(plus, it will help for you to format your code in your forum post as code instead of just text)

PeterWilson
Frequent Contributor

Hi Jerry

The reason that its writing the results out to "C:\GIS_DATA" is that the export path has been set to that directory manually. What you want to do is set the input_mxd and output_folder to become arguments that you can supply as the user of the script.  You need to set your input_mxd and output_folder variables to arcpy.GetParameterAsText to change the input and output arguments for each time you run the script as a tool within ArcGIS. I've cleaned your code a bit and gave explanations why.

'''
Created on Feb 17, 2016

Export Data Driven Pages

to jpegs

@author: PeterW
'''
# I've removed the unnecessary modules that you imported

# import modules and site-packages
import os
import arcpy

# set input and output arguments
input_mxd = arcpy.GetParameterAsText(0)
output_folder = arcpy.GetParameterAsText(1)
field_name = r"DIST_NAME"

# export each data driven page out as a jpeg 
mxd = arcpy.mapping.MapDocument(input_mxd)
for i in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = i
    row = mxd.dataDrivenPages.pageRow
    # I've created a variable page_name to store the current page name
    page_name = row.getValue(field_name)
    arcpy.AddMessage("Processing {}".format(page_name))
    # I've used os.path.join to join the path and file name for the output jpegs
    arcpy.mapping.ExportToJPEG(mxd, os.path.join(output_folder,  page_name + ".jpg"))
del mxd
JerryGarcia1
Deactivated User

Thanks for all your help!

0 Kudos
PeterWilson
Frequent Contributor

Pleasure Jerry

0 Kudos
gcastro
New Contributor

Hi, could you explain me how to set the input and output parameters?

0 Kudos