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

asked Sep 29, 2016 at 14:46
1
  • Can you edit your question an add the actual error message? Commented Sep 29, 2016 at 14:53

2 Answers 2

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

answered Sep 29, 2016 at 15:09
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)
answered Sep 29, 2016 at 15:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.