0

I am trying to export a pdf for each mxd in a folder. So far for code I have:

import arcpy, os
Workspace = r"FilePath"
arcpy.env.workspace = Workspace
arcpy.env.overwriteOutput=True
for file in arcpy.ListFiles("*.mxd"):
    mxd_path = os.path.join(Workspace,file)
    mxd = arcpy.mapping.MapDocument(mxd_path)
    outpdf = file + ".pdf"
    arcpy.mapping.ExportToPDF(mxd,outpdf)
    mxd.save()

I receive the error: Runtime error Traceback (most recent call last): File "<string>", line 5, in <module> 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 1156, in ExportToPDF layout.exportToPDF(*args) AttributeError: PageLayoutObject: Error in executing ExportToPDF

I am confused as to why the ExportToPDF is failing.

  • 2
    Can the offending map document be opened and exported manually? Are the mxd documents the same version as your ArcGIS version? Perhaps the mxd itself is broken or otherwise unreadable.. the error seems to occur with the PageLayoutObject which means it may be nothing you've done. The double extension (.mxd.pdf) could also be a problem, try replacing all the . with _ like outpdf = file.replace('.','_') + '.pdf' to 'sanitize' the name just to eliminate that as a potential problem. – Michael Stimson Aug 24 '16 at 22:03
  • 2
    I agree with @MichaelMiles-Stimson - AttributeError: PageLayoutObject makes it seem like an issue with the MXD rather than your script. When I test your script as-is (other than replacing with an actual path) I get no errors at all, and it happily creates files with filename.mxd.pdf which while untidy and potentially confusing, shouldn't be the cause of this issue – Midavalo Aug 25 '16 at 6:46
  • 2
    Also why do you have the mxd.save() in there? You're not modifying the MXD, so shouldn't need to save it – Midavalo Aug 25 '16 at 6:48
1

There are at least two potential issues.

  • the variable Workspace has been set to r"FilePath" which is not valid. If that is just hiding a full path name then I think it would be helpful to either show us that or to copy your MXDs into a folder like C:\temp so that you can
  • I'm suspicious as to whether outpdf is specifying valid values for your PDF files so try adding a print statement to check its value on each iteration
  • Yes Workspace = r"FilePath" is not a valid directory path. You can really help yourself out by using an error handler. Try using the traceback example as demonstrated here:pro.arcgis.com/en/pro-app/arcpy/get-started/… – GBG Aug 24 '16 at 22:31
  • 2
    @GBG That documentation is for ArcGIS Pro rather than Desktop, and although it may apply equally well I think there is no guarantee. In any event, when presenting code snippets here I think it is best NOT to include any try/except statements because they can mask otherwise helpful error messages. I reserve adding such tracebacks for using post getting the test cases working. – PolyGeo Aug 24 '16 at 23:01
  • Below is the link to the traceback code. It is the same in 10.3 and Pro. I have to disagree that the traceback can mask otherwise helpful error messages, this example returns both arcpy errors and, if necessary, more detailed Python errors. Do you have a specific example of masking?desktop.arcgis.com/en/arcmap/10.3/analyze/python/… – GBG Aug 25 '16 at 17:12
  • @GBG Anytime a try/except/traceback is poorly implemented, but the real issue is that for code snippets presented here we are looking for MCVEs. Any problems implementing tracebacks would be on-topic at Stack Overflow rather than here. – PolyGeo Aug 25 '16 at 21:36

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.