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 the previous question below

enter link description here

Where it details the ESRI , help page for data managment

enter link description here

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 doign wrong. I m not sure about setting up the parameters for an actual tool . i was wondering if maybe someone had already done this.

  • I think you must have accidentally deleted the URLs of the pages you were wanting to link to. You can use the edit button to go back in and fix them. – PolyGeo Jan 20 '15 at 12:04
3

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()
  • thank you Andy , i have tried to run that around an hour ago. Do you remember how long it took to process. So far it has done one map. It is probably the map has some issues. thansk for you help – Jack Walker Jan 20 '15 at 13:56
  • It usually took around 2 minutes per mxd. Do your mxds have a lot of layers? The command prompt should update with what it is doing. – AndyHarney Jan 20 '15 at 14:21
  • for the last part of your first comment: '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' how do i add ArCGIOS TO THE SYSTEM path . I Did not check for this so maybe this is the error – Jack Walker Jan 20 '15 at 15:13
  • Assuming you have Admin rights to the machine your on and its Windows 7, hold Win + Pause to bring up the System window. On the left will be "Advanced system settings", hit that and click the button "Environment Variables...". In the bottom part of the dialog for "System variables" find "Path" and click edit. Add the location to the Python bundled with ArcGIS - C:\Python27\ArcGIS10.2. Be sure to separate entries with ; – AndyHarney Jan 20 '15 at 16:01
  • thanks andy , that was excatly what i was looking for. thanks for your help – Jack Walker Jan 24 '15 at 21:23
4

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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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