3

I am looking for some help or if someone has a script or tool to loop through a given folder of MXD's and Create MPK files with the same name.

i don t have so much experience of creating scripts , or using the Python window. I found ArcGIS 10 - need a tool/script to batch save multiple MXD files to layer package

where it details the Esri help page for data managment

I tried inputting the Python window code changing the code for my examples but I keep getting different error message. I am not sure what i am doing wrong. I m not sure about setting up the parameters for an actual tool . i was wondering if maybe someone had already done this.

0

2 Answers 2

5

Take a look at this page:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//0017000000q5000000

I gave you the 10.0 link based on your tag.

# Name: PackageMap.py
# Description:  Find all the map documents that reside in a specified folder and create map packages for each map document.
# import system modules
import os
import arcpy

from arcpy import env

# Set environment settings
env.overwriteOutput = True
env.workspace = "C:/arcgis/ArcTutor/Editing" 

# Loop through the workspace, find all the mxds and create a map package using the same name as the mxd
for mxd in arcpy.ListFiles("*.mxd"):
    print "Packaging " + mxd
    arcpy.PackageMap_management(mxd, os.path.splitext(mxd)[0] + '.mpk', "PRESERVE", "CONVERT_ARCSDE", "#", "ALL")
4

Here is a script I wrote, based on the example Package Map (Data Management).
It has a few changes you'd need to be aware of. It removes any rasters from the mxds and it sets the required Summary, Description & Tags to the name of the MXD. This will change your source files so be sure to back them up.
It also just looks into a single directory and creates a small CSV file detailing any errors with certain mxds.
To stop rasters from being removed comment out lines 54-58.

This script is ran from the windows command line with python <scriptname>.py so you will need to ensure that the python.exe is included with ArcGIS is added to your system PATH. Most likely C:\Python27\ArcGIS10.2

# Import the required python modules
import arcpy
from arcpy import env
import os
import os.path
import glob
import string
import traceback
# Set some variables for the input MXDs
print 'Setting Paths'
path = raw_input('File Path to Folder Containing MXDs : ')
print path
mxdList = glob.glob(os.path.join(path, '*.mxd'))
print 'Paths Set'
env.workspace = path
arcpy.env.scratchWorkspace = path
env.overwriteOutput = True
arcpy.gp.overwriteOutput = True
outputcsv = path + "OutputLog_MXDs.csv"
outFile = open(outputcsv, "w")
outFile.write("MXD" + "," + "Status" + "," + "Error" + "\n")
successcount = 0
failcount = 0

# Module to set Descriptions, Summary and Tags for the mxd.
for filename in mxdList:
    try:
        mxdname = filename
        mxd = arcpy.mapping.MapDocument(filename)
        mxdleft = str(string.lstrip(filename, path))
        mxdname = str(string.rstrip(mxdleft, ".mxd"))
        print 'Processing ' + mxdname + '\n'
        mxd.description = mxdname
        print "Description = " + mxd.description
        mxd.summary = mxdname
        print "Summary = " + mxd.summary
        mxd.tags = mxdname
        print "Tag = " + mxd.tags + '\n'
        # Save the MXD
        mxd.save()
        # Now Package the mxd
        # Read Data Frames, Identify Raster Layers and Remove them.
        print 'Reading Data Frames...'
        DF = arcpy.mapping.ListDataFrames(mxd)
        print '...Found ' + str(len(DF)) + ' Data Frames' + '\n'
        for DF in arcpy.mapping.ListDataFrames(mxd):
            DFname = DF.name
            print "Processing - " + str(DFname)
            lyrs = arcpy.mapping.ListLayers(mxd)
            for lyrs in arcpy.mapping.ListLayers(mxd):
                if lyrs.isBroken:
                    print lyrs.name + " is Broken."
                    break
                if lyrs.isRasterLayer:
                    print "Found Raster - " + str(lyrs)
                    # Part to actually remove the raster, uses the data frame (DF) and the layer (lyrs)
                    arcpy.mapping.RemoveLayer(DF, lyrs)
                    print str(lyrs) + " Removed"
        # After the changes made, we need to save the MXD.
        mxd.save()
        print '\n' + "All Rasters Removed"
        print "\n" + "Packaging " + mxdname
        arcpy.PackageMap_management(filename,
                                    os.path.splitext(filename)[0] + '.mpk', "CONVERT", "CONVERT_ARCSDE", "#", "ALL")
        print mxdname + " Packaged Successfully"
        successcount += 1
        outFile.write(mxdname + "," + "Packaged" + "," + "None" + "\n")
        del mxd, mxdname, DF
    except Exception as e:
        print "-----"
        print "Error"
        traceback.print_exc()
        error = str(e.message.split('\r\n'))
        print "Problem With " + mxdname
        print "Ignoring Error, Like A Boss"
        outFile.write(mxdname + "," + "Not Packaged" + "," + error.replace('\n', ' ') + "\n")
        failcount += 1
        continue

# End Statement to display basic statistics.
print "\n"
print "Finished"
print str(successcount) + " MXDs Packaged Successfully - " + str(failcount) + " MXDs Failed"
print "Check Output Log For Details"
# Release the CSV logfile
outFile.close()
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.