Select to view content in your preferred language

Python Crashes when Running ExportToPDF(), ExportToPNG(), ExportToJPEG()

6999
12
Jump to solution
05-06-2015 06:36 PM
FrankMancini
Regular Contributor

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)
   
0 Kudos
1 Solution

Accepted Solutions
FrankMancini
Regular Contributor

Found a work around through another forum...

Python Crashes when Running ArcPy's ExportToPDF(), ExportToPNG(), ExportToJPEG() - Geographic Inform...

import multiprocessing

and replace the function call on line 34 "exportAISMap(mxd_path, pdf_path)" with ...

if __name__ == '__main__':
    multiprocessing.freeze_support()
    p = multiprocessing.Process(target = exportAISMap, args = (mxd_path, pdf_path))
    p.start()
    p.join()

This code would not run in Pythonwin, but worked from the command line.  Took about a minute to produce each map.

Python still crashed occasionally for no reason (Error: "python.exe has stopped working") but the script picked right back up where it left off.  With some kicking to get it started again it successfully produced all the maps in the folder.

View solution in original post

0 Kudos
12 Replies
SepheFox
Deactivated User

Hey Frank, I moved this to the Python subgroup because I thought you would have better luck. I hope you don't mind.

CarlSunderman
Frequent Contributor

You're trying to export to pdf? Here's a script i wrote for jpg and pdf export of all mxd's in a directory. You can hard code the values if you want and just add a png option at the end if you need to

import arcpy, os
inputPath = arcpy.GetParameterAsText(0)
outputPath = arcpy.GetParameterAsText(1)
outputType = arcpy.GetParameterAsText(2)


#Loop through each MXD file
for filename in os.listdir(inputPath):
    fullpath = os.path.join(inputPath, filename)
    if os.path.isfile(fullpath):
        if filename.lower().endswith(".mxd"):


            #Reference MXD and export
            mxd = arcpy.mapping.MapDocument(fullpath)
            fixedFilename = filename.strip( '.mxd' );
           
            if outputType == "jpg":
                arcpy.mapping.ExportToJPEG(mxd, outputPath + "//" + fixedFilename + ".jpg")
            else:
                arcpy.mapping.ExportToPDF(mxd, outputPath + "//" + fixedFilename + ".pdf")
FrankMancini
Regular Contributor

Thanks for the response but how is that fundamentally different from my original code?  I see that you added an additional parameter to allow user to choose export file type but I'm not concerned with that.   

The issue I am having is not with writing working code, it is that python crashes prematurely and inconsistently on 2 different machines running 10.2.2 for no logical reason when using the arcpy.mapping.Export functions. 

Did this code function properly for you in 10.2.2?  How many output maps have you successfully generated in one run?  Have you seen the same issue I am describing?  If so do you have a workaround other than starting the code again with the file it quit on?

I tested your code and it successfully made 8 pdf maps before crashing python on my machine.

0 Kudos
CarlSunderman
Frequent Contributor

I've used this with 10.2.2 on several hundred maps, in single and multiple directories as well and have not had any issues. If you want continue with your code or if there are issues with mine, I would use either print or print > text file to at least know when or where it breaks. Like i said, mine has never had any problems

FrankMancini
Regular Contributor

Ok thanks, good to know that it can work on hundreds of maps in 10.2.2.  I do have a log file running so I know where it fails.  I could rerun the code and pick up where I left off, I simply should not have to do this and your experience proves it.

So I know the where, driving me crazy that I cannot figure out why since it crashes python and ArcMap before it can return an error code.  I watched the Task Manager Performance tab while it runs and cpu usage ~45-65%, physical memory steady ~40%.

Maybe there is an issue with my install then?  Anything else come to mind that I can look into to try to fix this?  Spent way too many hours trying to print these maps to file already. 

0 Kudos
CarlSunderman
Frequent Contributor

where is it crashing?

0 Kudos
FrankMancini
Regular Contributor

Like I said in the title... arcpy.mapping.ExportToPDF() is crashing python.  I do not know where inside the export function it is failing or why because I cannot get a traceback or error message.  Python.exe is fatally crashing.

The script works properly when I comment out line 18 in my code above:

arcpy.mapping.ExportToPDF(mxd, out_path)

It does not always terminate on the same .mxd, hence the inconsistency I mentioned.

0 Kudos
IanMurray
Honored Contributor

I had a script like that once, it would process a random number features, then python would just quit, no error codes or anything, the script would just die.  I ended up doing the processing on a stronger computer and it managed to process the whole dataset.  Doesn't sound like that should be an issue for you though from the specs of the computer you are using so not sure, but I've had something similar happen. 

Also, do you really need to export at 300 DPI(default DPI value)?  It would process significantly faster and be more likely not to hang if you were exporting at a lower DPI.

CarlSunderman
Frequent Contributor

Just out of curiosity, try running mine. how many usually export before it fails? 1 or 2 or more like 90-100?

0 Kudos