Select to view content in your preferred language

Exporting Multiple Data Driven MXDs to PDFs using Python

5455
11
Jump to solution
08-13-2015 09:48 AM
anayoung
Regular Contributor

Hello,

I have several hundred "City Map" data driven MXDs that I would like to export to PDFs using Python. Right now we have to go into each MXD and export the PDFs; this takes time away from other stuff we could be doing. Is there some readily available code out there somewhere that would allow this function? I have been unable to find such a thing on the internet so far. I am new to coding so I am unable to do this by myself at this point. Your help would be much appreciated. Thanks, Ana

0 Kudos
11 Replies
FreddieGibson
Honored Contributor

Hi Ana,

You'll want to take note that there are two options that allow you to export to pdf. The one suggested by David is the ExportToPdf method of the mapping class. If you decide to use this class then you also have to included logic to advance to each page within the map document. The method I suggested is the exportToPDF method of the dataDrivenPages class. This option would handle all of the iteration logic for you. This method would provide you with parameters to allow you to both specify the pages to print or how many pdfs to create.

2015-08-14_0922.png

Instead of iterating through each page, your logic could be as simple as follows (Note: I wrote this without verifying if it works😞

import arcpy
import os   
    
def ExportToPDF(folder, pages, multiple_files, range_string="#"):
    """Walk specified directory to find files that in in *.mxd and export their
       data driven pages to a pdf."""
    
    # Walk the directory
    for root, dirs, files in os.walk(folder):
        for item in files:
            # Check if file is an mxd
            if not item.endswith(".mxd"):
                continue
            
            # Open mxd and hook into its data driven pages driver
            mxd = arcpy.mapping.MapDocument(path.join(root, item))
            ddp = mxd.dataDrivenPages
            
            # Specify the output path for the pdf
            pdf = os.path.join(root, item.replace(".mxd", ".pdf"))
            
            # desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/datadrivenpages-class.htm
            # Export the specified pages to the output pdf location
            ddp.exportToPDF(pdf, pages, range_string, multiple_files)
            
if __name__ == '__main__':
    ExportToPDF(r'C:\Path\To\Folder', 'ALL', 'PDF_SINGLE_FILE')
anayoung
Regular Contributor

Hi Freddie,

I apologize for the delay in response. I haven't had an opportunity to play with the code you posted for me yet. However, I want to thank you for walking me through it. As mentioned, I am new to scripting so I am not very good at picking out lines of code for use. Your code looks to be flexible and workable though. I will check out its functionality this week and post my findings. Thank you very much for your assistance - this novice appreciates the help. Until next time, Ana

0 Kudos