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.
1 Answer 1
There are at least two potential issues.
- the variable
Workspace
has been set tor"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 aprint
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– GBG2016年08月24日 22:31:39 +00:00Commented Aug 24, 2016 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.2016年08月24日 23:01:38 +00:00Commented Aug 24, 2016 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– GBG2016年08月25日 17:12:44 +00:00Commented Aug 25, 2016 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.2016年08月25日 21:36:04 +00:00Commented Aug 25, 2016 at 21:36
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 withfilename.mxd.pdf
which while untidy and potentially confusing, shouldn't be the cause of this issuemxd.save()
in there? You're not modifying the MXD, so shouldn't need to save it