Assertion error on ExportToPDF function.

1655
7
12-15-2011 08:25 AM
JasonTaylor
New Contributor
Hi, I'm new to ArcMapping with Python. For my first task I decided to do a batch export of maps. My code is simple but I get an error that has me stumped.

import arcpy, os
inpath="C:\\GIS\\MXD_dir\\"
outpath="C:\\Out_map_dir\\"
dirList = os.listdir(inpath)

arcpy.OverWriteOutput = 1

for item in dirList:
    print inpath + item
    arcpy.mapping.ExportToPDF(inpath + item, outpath + item[:-4] + ".pdf" )
    arcpy.mapping.ExportToJPEG(inpath + item, outpath + item[:-4] +".jpg" )



and here is my error...
Traceback (most recent call last):
  File "C:\GIS\Scripts\pdf_export.py", line 9, in <module>
    arcpy.mapping.ExportToPDF(inpath + item, outpath + item[:-4] + ".pdf" )
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 179, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\mapping.py", line 790, in ExportToPDF
    assert isinstance(map_document, MapDocument)
AssertionError


Any help or insight would be appreciated. Thanks!
Tags (2)
0 Kudos
7 Replies
MikeMacRae
Occasional Contributor III
You might be having issue joining your path name to your file name. try something like:

arcpy.mapping.ExportToPDF(inpath + "\\" + item, outpath + "\\" + item[:-4] + ".pdf" )
0 Kudos
JasonTaylor
New Contributor
I thought I took care of that in the when I set the path in in the path variable by adding "\\" there. I tried it any way and got the same error. Thanks though.
0 Kudos
MikeMacRae
Occasional Contributor III
I have a feeling it has something to do with the way your are looping through your mxd's using dirList = os.listdir(inpath).

I have a similar code to flip through a directory of .mxd's and exports to pdf. Try this:

import arcpy, glob, os, sys

inpath = r"C:\GIS\MXD_dir\"
outpath=r"C:\Out_map_dir\"

for filename in glob.glob(os.path.join(inPath, "*.mxd")):

        fullpath = os.path.join(inPath, filename)
        mxd = arcpy.mapping.MapDocument(filename)

        if os.path.isfile(fullpath):
            basename, filename2 = os.path.split(fullpath)
            shortname, extension = os.path.splitext(filename2)

            arcpy.mapping.ExportToPDF(mxd, outpath + "\\" + shortname, "PAGE_LAYOUT")

            del mxd
            del shortname
            del basename
            del filename2
            del extension
            
del filename
0 Kudos
JasonTaylor
New Contributor
Yes, this works great. Thanks for helping me through this.

I need to research more about glob and the os.path functions, because I'm not sure why it won't work. Your definitely right though, there is a difference.

Thanks again for the help!
0 Kudos
JeffBarrette
Esri Regular Contributor
It wasn't working because the first parameter in your original ExportToPDF and ExportToJPG functions were using an MXD path, not an MXD reference.

Jeff
0 Kudos
JasonTaylor
New Contributor
Ah, I see. This was the crucial line I was missing

mxd = arcpy.mapping.MapDocument(filename)


Adding it, my code works great!

import arcpy, os
inpath="C:\\GIS\\MXD_dir\\"
outpath="C:\\Out_map_dir\\"
dirList = os.listdir(inpath)

for item in dirList:
        mxd = arcpy.mapping.MapDocument(inpath + item)
        arcpy.mapping.ExportToPDF(inpath + item, outpath + item[:-4])
        arcpy.mapping.ExportToJPEG(inpath + item, outpath + item[:-4])


I also removed the the output extensions, not sure if that matters though.

Thanks again!
0 Kudos
DianeMcConnaughey
Occasional Contributor
Thank you!  I was missing  mxd = arcpy.mapping.MapDocument(mxdname), and this line before using arcpy.mapping.ExportToPDF
fixed the problem.
0 Kudos