Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to writing a python script that will automate exporting a set of stock maps to JPEG from their mxd files, and I'm having some strange issues getting there.

I've alternatively been getting a Visual C++ Runtime error saying python experienced an abnormal program termination, or the program throws an exception saying "AttributeError: PageLayoutObject: Error in executing ExportToJPEG"

I've tried two main ways of running this process. In one I loop through an array of directories and pull the individual mxd files into the export command, as below:

for line in Direct:
    path = str(line) + "*.mxd"

    listing = os.listdir(path)
    for infile in glob.glob(path):
        mxd = arcpy.mapping.MapDocument(infile)
        infile = infile.split("\\")[-1]
        infile = infile.rstrip('.mxd')
        infile = infile + ".jpg"

        project = os.path.join(destpath, infile)
        arcpy.mapping.ExportToJPEG(mxd, project, resolution = 200)

        del mxd

In the other form, I copy the arcpy.mapping.ExportToJPEG() line over and over with the parameters for each map specified, as below:

mxd = arcpy.mapping.MapDocument(r"Y:\Maps\map1.mxd")
project = "C:/Maps/map1.jpg"
arcpy.mapping.ExportToJPEG(mxd, project, resolution = 200)

mxd = arcpy.mapping.MapDocument(r"Y:\Maps\map2.mxd")
project = "C:/Maps/map2.jpg"
arcpy.mapping.ExportToJPEG(mxd, project, resolution = 200)

mxd = arcpy.mapping.MapDocument(r"Y:\Maps\map3.mxd")
project = "C:/Maps/map3.jpg"
arcpy.mapping.ExportToJPEG(mxd, project, resolution = 200)

I've tried multiple tests to see if my syntax is wrong, and if the script is using too many resources and crashing, but haven't been able to figure out what my issue is.

The weird part is I can't seem to find any pattern or reason to the maps I'm getting these errors on. It seems like it changes every time I alter my script.

share|improve this question

You need to include empty quotes for parameters you aren't changing. Since resolution is the 6th parameter, you shouldn't place it directly after parameter 2 (out_jpeg). The empty quotes are place markers.

Example: arcpy.mapping.ExportToJPEG(mxd, project, "", "", "", resolution = 200)

You don't need to put quotes in for parameters after the last one you have defined.

Python isn't smart enough to figure out the order of parameters without explicitly telling it what is what. In your case, Python throws an error because you are defining parameter 6 in parameter 3's location.

Edit: I did some research and learned that you don't need to put in parameter space fillers if you explicitly state the parameter name in your code. I.e., since you put "resolution = 200" instead of just "200", Python should be able to figure out which parameter you are talking about. It shouldn't hurt to put in the empty quotes, but it isn't necessary if you explicitly state your parameter. That's why your original code was working fine.

Something you may test is explicitly adding default optional parameters to see if that works. For example,

arcpy.mapping.ExportToJPEG(mxd, project, data_frame = "PAGE_LAYOUT", resolution = 300)

worked on my machine. I'm not sure why the code works with some mxds and not others. My guess is that some mxds don't like the parameter syntax for some reason. Sorry I couldn't help more. Maybe some Python gurus can chime in.

share|improve this answer
This makes sense, but it's giving me the error after working fine on 10 or more maps already. Let's find out if it works! – AlexGilvarry Oct 16 '12 at 16:52
I don't have an explanation for that. I'm just giving a possible explanation for why you are getting the specific error you mentioned in your OP. Why it didn't have a problem with it before is beyond me. ESRI software is somewhat notorious for random bizarre effects. Have you changed ArcGIS versions recently? – Baltok Oct 16 '12 at 16:56
When I added tbe empty quotes to the script it actually gave me this exception: "AttributeError: PageLayoutObject: Error in parsing arguments for ExportToJPEG" I dunno what to do with that... As far as changing versions recently, I upgraded to 10.1 when it came out a few months ago. – AlexGilvarry Oct 18 '12 at 15:42
Well, I did some testing on my ArcGIS 10.0 machine and I get the same error you do. I don't know why it has a problem with the double quotes considering ESRI specifically mentions you can use it in their Python help. Also, I goofed a little on my original answer. See above edits. – Baltok Oct 18 '12 at 18:25
"Python isn't smart enough to figure out the order of parameters without explicitly telling it what is what." No, this has nothing to do with Python's "smarts." The problem is with the design of the ExportToJPEG() function: it probably should let some of its inputs be optional keyword arguments instead of requiring empty strings. – sgillies May 17 at 15:30

This is old but I had the same error "PageLayoutObject: Error in executing ExportToJPEG". The problem for me was that I was trying to write to a folder that didn't exist. Once I created the folder, it worked fine. This also makes me think that if you had some other kind of permissions problem that didn't let you write to the output file it might also be expressed as this kind of an error.

share|improve this answer

Try this code:

import arcpy,os,sys
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
for mxd in arcpy.ListFiles("*.mxd"):
    print mxd # print list of mxd's in the folder
    mapdoc = arcpy.mapping.MapDocument(r"C:\Project\\" + mxd)
    df = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[0]
    arcpy.mapping.ExportToJPEG(mapdoc,r"C:\Project\out\\"+os.path.basename(mapdoc.filePath).replace ('.mxd','') +".jpg")
    print 'ExportToJPEG'
del mxd
share|improve this answer
import arcpy, os
import arcpy.mapping

PATH = r"D:\test" ## Path with the mxds
arcpy.env.workspace = PATH

listMXD = arcpy.ListFiles("*.mxd")
for item in listMXD:
    mxd = arcpy.mapping.MapDocument(os.path.join(PATH, item))
    outjpg = item[:-4] + ".jpj"
    arcpy.mapping.ExportToJPEG(mxd, outjpg, resolution=300 )
del mxd
share|improve this answer
Some context or additional information about why this code works or works better would improve the quality of this answer. – Chris W Nov 10 '15 at 20:21

It doesn't seem any of the answers above answered the question, even though each may contain some useful information.

This could be a memory issue. mxd and df could take up huge memories, especially if any huge datasets are involved. When looping through a lot of mxds to export, promptly clear temporary mxds and dfs would be necessary, such as

del mxd

del df

within each loop.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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