HOW TO

Export map documents to PDF using Python

Last Published: April 25, 2020

Summary

Instructions provided describe how to export multiple map documents (.mxd) to PDF using a Python script.

Procedure

The following code sample accesses a workspace, creates a list of existing .mxd files in the workspace, iterates through each .mxd, and exports to PDF.

Code:

import arcpy, os

arcpy.env.workspace = ws = r"C:\\My_Folder"

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:
    
    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
    pdf_name = mxd[:-4] + ".pdf"
    arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
 
del mxd_list

To set the desired resolution, width, and height, insert the following variables after setting the workspace, and insert the new parameters into the ExportToPDF function. The following code is a sample of how the full script looks:

Code:

import arcpy, os

arcpy.env.workspace = ws = r"C:\\My_Folder"

resolution = "600"
width = 640
height = 480

mxd_list = arcpy.ListFiles("*.mxd")

for mxd in mxd_list:
   
    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
    pdf_name = mxd[:-4] + ".pdf"
    arcpy.mapping.ExportToPDF(current_mxd, pdf_name, resolution, width, height)

del mxd_list
  
Note:
The code can also be used as a standalone Python script. This enables users to convert multiple MXD files without opening ArcMap.

Article ID:000012420

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic