Skip to content

Instantly share code, notes, and snippets.

@morehavoc
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morehavoc/0d38e6efb9ee29d7d0a1 to your computer and use it in GitHub Desktop.
Save morehavoc/0d38e6efb9ee29d7d0a1 to your computer and use it in GitHub Desktop.
An extension of export_mxd.py that provides an ESRI toolbox that does the same thing as the export_mxd.py script.
import arcpy
import os.path
class Toolbox(object):
def __init__(self):
self.label = "Simple Export Toolbox"
self.alias = "ExportMXD"
# List of tool classes associated with this toolbox
self.tools = [ExportMXD]
class ExportMXD(object):
def __init__(self):
self.label = "Export MXD"
self.description = "Exports all MXDs in the folder (and sub-folders) specified here." + \
"The newly exported file will have the same name as the MXD and reside" +\
"in the same folder as the MXD. The tool returns the folder as output."
def getParameterInfo(self):
#Define parameter definitions
# Input Features parameter
input_folder = arcpy.Parameter(
displayName="Input Folder",
name="input_folder",
datatype="DEFolder",
parameterType="Required",
direction="Input")
# Sinuosity Field parameter
export_type = arcpy.Parameter(
displayName="Export Type",
name="export_type",
datatype="GPString",
parameterType="Required",
direction="Input")
export_type.filter.type = "ValueList"
export_type.filter.list = ["AI",
"BMP",
"EMF",
"EPS",
"GIF",
"JPEG",
"PDF",
"PNG",
"SVG",
"TIFF"]
# Derived Output Features parameter
output_folder = arcpy.Parameter(
displayName="Output Folder",
name="output_folder",
datatype="DEFolder",
parameterType="Derived",
direction="Output")
output_folder.parameterDependencies = [input_folder.name]
parameters = [input_folder, export_type, output_folder]
return parameters
def isLicensed(self): #optional
return True
def updateParameters(self, parameters): #optional
return
def updateMessages(self, parameters): #optional
return
def execute(self, parameters, messages):
input_folder = parameters[0].valueAsText
export_type = parameters[1].valueAsText.upper()
## Set the output
parameters[2].value = input_folder
os.path.walk(input_folder, export_mxd, export_type)
return
def export_mxd(arg, dirname, filenames):
## Loop over each filename returned in the filenames list, and export it
## if it is a MXD file
for filename in filenames:
## Ensure that the file is really a MXD (has .mxd file extension)
if filename[-3:].upper() != 'MXD':
continue
## Create the source file path
source_file = os.path.join(dirname, filename)
## Load the MXD
source_mxd = arcpy.mapping.MapDocument(source_file)
## Find the appropriate export method on the arcpy.mapping module
export_function = getattr(arcpy.mapping, "ExportTo" + arg.upper(), None)
## now export
if not export_function:
arcpy.AddError("***Something went wrong, we couldn't export the map to a {}".format(arg))
raise Exception("Unknown export type ({})".format(arg))
## Loop over all data frames in the mxd, to export each one
data_frames = arcpy.mapping.ListDataFrames(source_mxd)
for data_frame in data_frames:
## Loop over each bookmark in the data frame
bookmarks = arcpy.mapping.ListBookmarks(source_mxd, "*", data_frame)
for bookmark in bookmarks:
## zoom to each bookmark and export
data_frame.extent = bookmark.extent
destination_file = os.path.join(dirname, "{}_{}_{}.{}".format(filename[:-4], data_frame.name, bookmark.name, arg))
arcpy.AddMessage("export {} to {}".format(source_file, destination_file))
export_function(source_mxd, destination_file, data_frame)
if len(bookmarks) == 0:
## no bookmarks, but we should still print the data_frame
destination_file = os.path.join(dirname, "{}.{}".format(filename[:-4], arg))
arcpy.AddMessage("export {} to {}".format(source_file, destination_file))
export_function(source_mxd, destination_file)
arcpy.AddMessage("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment