I am attempting to loop through a folder of ~400 .mxd's and save each to a .pdf in another folder. All tested arcpy.mapping export functions (pdf, jpeg & png) kill python without a chance to debug or any indication why. Running script in PythonWin gives error message "PyWin32 has stopped working." When run from python prompt within ArcMap, program just closes.

Script behaves inconsistently, sometimes makes 10 maps before crashing, other times only 1 or 2. All mxds were created from the same arcpy.mapping script and all are valid (can open in ArcMap and export without issues). There is an ArcGIS Online basemap layer in each map (Oceans), I tried unchecking this layer in the first few maps and it did not have an effect on the script, python still crashed.

Tested script on 2 different laptops with same result. Both running ArcGIS Desktop 10.2.2, Python 2.7...

Windows 7 Pro 64-Bit SP1, Intel Core i5, 8GB Dual-Channel RAM, Intel HD Graphics 4000

Windows 7 Pro 64-Bit SP1, Intel Core i7, 16GB Dual-Channel RAM, Intel HD Graphics 4000, 2048MB NVIDIA Quatro K2000M

Saw a few similar posts here and on other message boards, but no working solutions.

I had no problem generating 400 maps using arcpy.mapping and cannot believe that I cannot save them out to a file. Is this a bug?

import arcpy, os  

ws = r"C:\temp"  
map_folder = os.path.join(ws, "maps")  
pdf_folder = os.path.join(ws, "pdfs")  

arcpy.env.workspace = map_folder  
arcpy.env.overwriteOutput = True  

# generate list of map documents in folder to loop through  
map_list = arcpy.ListFiles("*.mxd")  

def exportAISMap(mxd_path, out_path):  
    mxd = arcpy.mapping.MapDocument(mxd_path)  
    # arcpy.mapping.ExportToJPEG(mxd, out_path)  
    # arcpy.mapping.ExportToPNG(mxd, out_path)  
    arcpy.mapping.ExportToPDF(mxd, out_path)  
    print "Exported map file: " + str(out_path) + "\n"  
    # mxd.save()  
    del mxd  

print "Saving out " + str(len(map_list)) + " map documents\n"     

for map_file in map_list:  
    print map_file  

    mxd_path = os.path.join(map_folder, map_file)  

    pdf_file = map_file.replace(".mxd", ".pdf")  
    pdf_path = os.path.join(pdf_folder, pdf_file)     

    exportAISMap(mxd_path, pdf_path)  
  • My initial guess is that memory leak is occurring. Have you opened up task manager to monitor your memory while the script is processing?
    – papadoo
    May 7, 2015 at 21:50
  • @papadoo i did review the task manager performance tab while it runs: cpu usage ~45-65%, physical memory steady ~40%. i was thinking that it was a memory issue as well and added a gc.collect() to the end of the loop, but didn't help
    – Frank
    May 7, 2015 at 22:12

2 Answers 2


i don't trust the gc module as much as I do spawning a separate process. The separate process will do the work and will then be killed after the join(). Try adding:

from multiprocessing import process

to the top of your script then replace:

exportAISMap(mxd_path, pdf_path)


p = Process(target = exportAISMap, args = (mxd_path, pdf_path))
  • Thanks for pointing me in the multiprocessing direction. Had never used it before. Took a little tweaking and handholding but got it to finish the job. Reviewing the pdf's now. Had to make a few small changes to get it run. Would not run in Pythonwin but worked from command line. Occasionally crashed python but kept chugging along where it left off once I cancelled out the error message (python.exe has stopped working). from multiprocessing import Process ... if __name__ == '__main__': multiprocessing.freeze_support() p = Process() ...
    – Frank
    May 8, 2015 at 18:41
  • Sorry about the code formatting, cannot figure out how to make linebreaks in the comments, is 2 spaces not possible here? Pdf's look correct, took about a minute to generate each pdf. Thanks for the nudge in the right direction!
    – Frank
    May 8, 2015 at 19:22

You have to remeber that the arcpy module is really just a wrapper around a huge COM library. Most of the time it usually works but every once in a while you run into something like this. My guess is that the problem is in the underlying COM library, in which case this could be difficult to deal with.

One thing you can try, although this is a long shot, is to put your outputs into a completely separate path than the mxd files. So instead of C:\Temp\pdfs something like C:\Outputs\pdfs. You may also want to look into Data Driven Pages to see if those tools would allow you to create your multiple output maps without using arcpy.

  • Thanks for the comment, data driven pages was not an option. Each map has the same template but the data source, scale and labels change. No way to index it. I also assume there is something wrong with the underlying code, no idea why though if other people are using this function without issue and I have not modified it in any way.
    – Frank
    May 8, 2015 at 19:25

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.