Export MXDs in all subdirectories

Posted by in ArcMap, Data, GIS, Mapbook

This is an answer to the question in my post on batch exporting mxd’s in a folder. James asked about exporting all mxds in all subdirectories of a root folder. Thanks James for distracting me from my work this afternoon to solve this :).

Below is the code to accomplish this. The first loop populates the mxdList list with the full path to each mxd present in any sub directory of the source path. The second loop creates the full path for the corresponding PDF to each mxd and exports them. There is some more elaboration on how the code works here the the post mentioned above, batch exporting mxd’s in a folder.

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
import os
import glob
 
source = r"C:\Maps"
mxdList = []
 
outLoc = source
 
for dirpath, subdirs, files in os.walk(source):
  for x in files:
    if x.endswith(".mxd"):
      mxdList.append(os.path.join(dirpath, x))
 
print mxdList
 
res = 300
qual = "BEST"
 
for mxd in mxdList:
  pdf = mxd.replace(".mxd",".pdf")
  MXDpath = mxd
  print MXDpath
  mapdoc = arcpy.mapping.MapDocument(MXDpath)
  PDFpath = pdf
  print PDFpath
  arcpy.mapping.ExportToPDF(mapdoc,PDFpath,"PAGE_LAYOUT",0,0,res,qual)
  del mxd

Here’s the folder setup used in the script above, and the resulting pdf files after running the script.

Capture

As a variation on this I also adjusted the script so that all the PDFs are exported to the same folder. All the PDFs will be exported to the folder defined in the outLoc variable.

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
import os
import glob
 
source = r"C:\Maps"
mxdList = []
 
outLoc = source
 
for dirpath, subdirs, files in os.walk(source):
  for x in files:
    if x.endswith(".mxd"):
      mxdList.append(os.path.join(dirpath, x))
 
print mxdList
 
res = 300
qual = "BEST"
 
for mxd in mxdList:
  pdf = os.path.basename(mxd).replace(".mxd",".pdf")
  MXDpath = mxd
  print MXDpath
  mapdoc = arcpy.mapping.MapDocument(MXDpath)
  PDFpath = os.path.join(outLoc,pdf)
  print PDFpath
  arcpy.mapping.ExportToPDF(mapdoc,PDFpath,"PAGE_LAYOUT",0,0,res,qual)
  del mxd

Here’s the results of this variation.

Capture2

5 Comments

  1. James
    November 20, 2015

    Thank you for the great answer. Exactly what I was looking for.

    Sorry for taking away from your afternoon work! Haha.

    Reply
    • Brian
      November 20, 2015

      Awesome! And no problem, it was funner than what I was doing anyway.

      Reply
  2. Sophie
    April 5, 2016

    Thanks, that worked great.

    Just a further question as my python is pretty limited. Do you know how I could do it so that any subfolders containing the words old or draft are not printed. For example in the mapping folder we might have a further folder called old_maps which have been superseded so i don’t want to bother printing any of them.

    Cheers,
    Sophie

    Reply
    • Brian
      April 6, 2016

      Hi Sophie, after the line [qual = “BEST”] replace the rest with:

      excludeDir = ‘old’

      for mxd in mxdList:
      if mxd.find(excludeDir)<1:
      print mxd
      pdf = os.path.basename(mxd).replace(".mxd",".pdf")
      MXDpath = mxd
      print MXDpath
      mapdoc = arcpy.mapping.MapDocument(MXDpath)
      PDFpath = os.path.join(outLoc,pdf)
      print PDFpath
      arcpy.mapping.ExportToPDF(mapdoc,PDFpath,"PAGE_LAYOUT",0,0,res,qual)
      del mxd

      Any mxd with 'old' in the path will be excluded. This is a very quick solution and note that if 'old' is anywhere in the path, the mxd won't be printed. I'll work on excluding the subdirs from the os.walk loop but I don't know how to do it without some research and testing.
      Indents may not be coming through, indent lines after a :

      Reply
  3. sam
    December 8, 2017

    Thank you very much for this script.

    i want to add some thing to it.I have a layer named “village” in that” field_name = village name”.
    highlight the village and zoom to it and then export to pdf can any one guide me on this.

    Reply

Leave a Reply