1

I am trying to make a tool for exporting pdfs from a directory: I found one online but it was very slow, I am trying to code it more efficiently. I am getting an AssertionError and I am not sure how to fix it.

import arcpy
import os

arcpy.env.workspace = r"C:\Users\jds\Desktop\MXD_Export_Test"

folderPath = r"C:\Users\jds\Desktop\MXD_Export_Test"

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

for mxd in mxdList:  
    saveName = mxd.split(".")[0] + ".pdf"
    savePath = os.path.join(folderPath, saveName) 
    arcpy.mapping.ExportToPDF(mxd, savePath)  

Error =

Traceback (most recent call last): File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec codeObject in main.dict File "C:\Users\jds\Desktop\Scripts\Export_PDF_From_Directory.py", line 14, in arcpy.mapping.ExportToPDF(mxd, savePath) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py", line 1148, in ExportToPDF assert isinstance(map_document, MapDocument) AssertionError

  • Can you edit your question an add the actual error message? – Brian Sep 29 '16 at 14:53
3

Your variable mxd as it stands is not a MapDocument object, it's just a path. You have to maybe add a line under the loop:

for mxd in mxdList:  
    md = arcpy.mapping.MapDocument(mxd)
    saveName = mxd.split(".")[0] + ".pdf"
    savePath = os.path.join(folderPath, saveName) 
    arcpy.mapping.ExportToPDF(md, savePath)  

See the documentation here: ExportToPDF

0

Ok,

I have figured it out, with help form Jvhowube.

Not only did I need a map object, but the map object needed to be the full file path.

for mxd in mxdList:
    fullPath = os.path.join(folderPath, mxd)
    md = arcpy.mapping.MapDocument(fullPath)
    saveName = mxd.split(".")[0] + ".pdf"
    savePath = os.path.join(folderPath, saveName)
    arcpy.mapping.ExportToPDF(md, savePath)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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