1

I have several MXDs that use Data-Driven-Pages to create an updated mapbook every month. 15-18 mapbooks and 163 pages each. This usually takes me two days and I'd like to automate this. I can export one with Python, but more than one is stumping me. I export one using this in Python:

map1 = arcpy.mapping.MapDocument("map1 mxd location")

map1.dataDrivenPages.exportToPDF(r"folder where I want pdf stored", "RANGE", "1-163","PDF_MULTIPLE_FILES_PAGE_NAME")

del map1

It works great for one map, but how can I export map1, map2, map3 etc.. to separate folders with one executed command?

Just to be clear, I know I need to specify each MXD and export location individually. What I'm looking to learn is how to enter all that information at once, double tap my enter button and walk away.

0

3 Answers 3

3

this looks like a problem where a for-loop would work. If you have a list of your MXDs (including path), you can loop over them.

listofmaps = open(r"C:\Users\maplist.txt","r")
for map in listofmaps:
   map1 = arcpy.mapping.MapDocument(map)
   map1.dataDrivenPages.exportToPDF(r"folder where I want pdf stored", "RANGE",    "1-163","PDF_MULTIPLE_FILES_PAGE_NAME")
   del map1

Each map in the text file will be output to the folder.

1
  • Thank you @Sam and everyone else for the replies. I've tried all 3 and this is the first one that worked for me. I'm sure I was doing something wrong with the other two. Here's what I ended up with if anyone is interested. >>> listofmaps = open(r"C:\Users\....ListofMaps.txt", "r") >>> for maps in listofmaps: ... Hse200 = arcpy.mapping.MapDocument("Hse200 mxd location") ... Hse200.dataDrivenPages.exportToPDF(r"Hse200 PDF destination") ... del Hse200 ... #repeat for all the maps you need ... break (I've learned break is very VERY important) Thanks again everyone!
    – Mike Kirn
    Commented Aug 14, 2015 at 15:55
2

This uses os.walk to find all MXDs within a root directory.

It uses os.path.join to combine the file path and filename, and creates an output PDF name within the directory the MXD is located.

import os
import arcpy

#change to the root of where all the MXDs are stored
workspace = "C:\\Projects"

for dirpath, dirnames, filenames in os.walk(workspace):
    for name in filenames:
        if "mxd" in name:
           output_name = str(name.split("mxd")[0]) + "pdf"
           output_path = os.path.join(dirpath, output_name)

           map_document = os.path.join(dirpath, name)

           mxd = arcpy.mapping.MapDocument(map_document)
           mxd.dataDrivenPages.exportToPDF(output_path, "ALL", multiple_files="PDF_MULTIPLE_FILES_PAGE_NAME")

           del mxd

So, for C:\\Projects\\Rebuild\\Imagery.mxd the output is C:\\Projects\\Rebuild\\Imagery.pdf

In your example, you have a RANGE set. To do this adds a bit more complexity (if it's different for each MXD), and you'll need to reference some file that has this information. In this example, I've set it to ALL.

0

You're going to want to use a python list and a for loop for this.

#List all maps here
maps = ["map1 mxd location", 
        "map2 mxd location", 
        "map3 mxd location"]

for map in maps:
    map1 = arcpy.mapping.MapDocument(map)
    #etc etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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