Batch export mxds in a folder

Posted by in ArcMap, GIS

Below is python to export all mxd’s in a given directory. I use this very often in my current workflow. This is used when data driven pages is either not appropriate or does not adhere to workflow standards. We need individual mxd’s for each map and each version of each map, individual maps can’t be trapped in one mxd.

To make this work for you, change the path on line 7 to the directory containing your mxds. Then, if desired, change the mxdList variable on line 11 to export a subset of mxds based on their filenaming. This example will export all mxds in the “CCC_Maps” directory.

The output location can also be changed on line 16. The default is the same as the input directory where the mxds live. To change this, comment line 15, uncomment line 16 and change the destination for the pdfs.

The export quality settings can be changed via lines 19 and 22. These are the defaults I use to maintain high quality pdfs.

This works in Python 2.7 and ArcGIS 10.2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#b greer 12/12/2014
 
import arcpy, os
from arcpy import env
 
#change to directory with mxds
workspace = r"D:\CCC_Maps"
env.workspace = workspace
 
#mxd list query here
mxdList = "*"+".mxd"
 
#change output directory for PDFs
#if outLoc is the same as the mxd location, uncomment line 13 and comment 14, else specify outLoc on line 14
outLoc = workspace
#outLoc = r"D:\"
 
#change res to desired value
res = 300
 
#change qual to desired level, choose: "BEST", "BETTER", "NORMAL", "FASTER", "FASTEST"
qual = "BEST"
 
mxds = arcpy.ListFiles(mxdList)
for mxd in mxds:
    pdf = mxd.replace(".mxd",".pdf")
    MXDpath = os.path.join(workspace,mxd)
    mapdoc = arcpy.mapping.MapDocument(MXDpath)
    PDFpath = os.path.join(outLoc,pdf)
    print mapdoc
    arcpy.mapping.ExportToPDF(mapdoc,PDFpath,"PAGE_LAYOUT",0,0,res,qual)
    del mxd

6 Comments

  1. Lisa
    October 19, 2015

    Thank you so much for sharing this! 🙂

    Reply
  2. James
    November 19, 2015

    Brian, thanks. This was exactly what I have been trying to figure out for a year now. I’m curious, is there a way to repeat this process for subfolders as well?

    ie.) Recursively print all .mxds in the subfolders as listed below:

    C:\\GIS\Year 5B
    >Zone 1
    > A.mxd
    > B.mxd
    >Zone 2
    > A.mxd
    > B.mxd

    etc.

    Would the os.walk function be used?

    Thanks again.

    Reply
    • Brian
      November 19, 2015

      Hey James,
      Yeah you would go to walk. You’d have to change some things for this to work, the MXDpath and PDFpath would have to reflect the input mxds in different sub folders and output location for each PDF.

      Reply
      • James
        November 19, 2015

        Sorry, I have no actual knowledge of how to use python. If I want process to work exactly as you have written, and then go to the next folder and repeat, I would assume the following should work if I’m able to define the “subfolder”, but I’m not sure how to do that so all subfolders fit the definition.

        >>> import arcpy, os
        >>> from arcpy import env
        >>> workspace = r”FOLDER LOCATION”
        >>> env.workspace = workspace
        >>> mxdList = “*”+”.mxd”
        >>> outLoc = workspace
        >>> res = 300
        >>> qual = “BEST”
        >>> mxds = arcpy.ListFiles(mxdList)
        >>> for mxd in mxds:
        pdf = mxd.replace(“.mxd”,”.pdf”)
        MXDpath = os.path.walk(workspace, subfolder, mxd)
        mapdoc = arcpy.mapping.MapDocument(MXDpath)
        PDFpath = os.path.walk(outLoc, subfolder, pdf)
        print mapdoc
        arcpy.mapping.ExportToPDF(mapdoc,PDFpath,”PAGE_LAYOUT”,0,0,res,qual)
        del mxd

        Are you able to point me in the correct direction by any chance?

        Reply
  3. Anonymous
    June 28, 2016

    modified code to work with data driven pages:

    ______________________________________________________

    #b greer 12/12/2014
    #http://www.bgcarto.com/batch-export-mxds-in-a-folder/
    #MODIFIED TO WORK FOR DATA DRIVEN PAGES

    import arcpy, os
    from arcpy import env

    #change to directory with mxds
    workspace = r”C:\Project”
    env.workspace = workspace

    #mxd list query here
    mxdList = “*”+”.mxd”

    #change output directory for PDFs
    #if outLoc is the same as the mxd location, uncomment line 13 and comment 14, else specify outLoc on line 14
    outLoc = workspace
    #outLoc = r”D:\”

    #change res to desired value
    res = 200

    #change qual to desired level, choose: “BEST”, “BETTER”, “NORMAL”, “FASTER”, “FASTEST”
    qual = “NORMAL”

    mxds = arcpy.ListFiles(mxdList)
    for mxd in mxds:
    pdf = mxd.replace(“.mxd”,”.pdf”)
    MXDpath = os.path.join(workspace,mxd)
    mapdoc = arcpy.mapping.MapDocument(MXDpath)
    PDFpath = os.path.join(outLoc,pdf)
    print mapdoc
    #ADDED PARAMETER NAMES AND VALUES
    arcpy.mapping.MapDocument(MXDpath).dataDrivenPages.exportToPDF(out_pdf=MXDpath, page_range_type=”ALL”, resolution=res, image_quality=qual, layers_attributes=”NONE”)
    del mxd

    Reply

Leave a Reply