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/c0f43ec5ad0700cc6cba to your computer and use it in GitHub Desktop.
Save morehavoc/c0f43ec5ad0700cc6cba to your computer and use it in GitHub Desktop.
Quick script to convert all the MXD's in a folder into a particular export type, like a PDF, or JPEG. Automatically picks up all of the files within any folder below the folder specified.
import arcpy
import os.path
base_directory = r"C:\temp"
export_type = "PDF"
## Ensure that the export_type exists
if not hasattr(arcpy.mapping, "ExportTo" + export_type.upper()):
print ("*** You must specify an export type of one of: ***")
print ("*** AI, BMP, EMF, EPS, GIF, JPEG, PDF, PNG, SVG, TIFF***")
exit()
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)
## Create the destination_file path by adding the
## type of export to the end of the file path
destination_file = os.path.join(dirname, filename + "." + arg)
print("export {} to {}".format(source_file, destination_file))
## 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 export_function:
export_function(source_mxd, destination_file)
print ("DONE!")
else:
print ("***Something went wrong, we couldn't export the map to a {}".format(arg))
print ("")
## A special python function that will recusivly search through a directory
## In each directory, the method "export_mxd" is called with the directory
## name and a list of filenames in that directory
## Check out: https://docs.python.org/2/library/os.path.html#os.path.walk
## afor more info
os.path.walk(base_directory, export_mxd, export_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment