Hello,
I am new to coding. I am trying to write a script that will allow me to export multiple map documents to PDF using Python. I found some code to do just that at the following ESRI help link 44242 - Export map documents to PDF using Python . However when I try to run it I get the below Runtime Error message. The only thing I changed from the original code was the workspace pathway. Thank you very much, Ana
>>> import arcpy, os
...
... arcpy.env.workspace = ws = "C:\Users\00177153\Desktop\ArcPy"
...
... mxd_list = arcpy.ListFiles("*.mxd")
...
... for mxd in mxd_files:
...
... current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
... pdf_name = mxd[:-4] + ".pdf"
... arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
...
... del mxd_files
...
Runtime Error
Traceback (most recent call last):
File "<string>", line 7, in <module>
NameError: name 'mxd_files' is not defined
Solved! Go to Solution.
I tested this code, try this
import arcpy, os arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy" mxd_list = arcpy.ListFiles("*.mxd") for mxd in mxd_list: current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd)) pdf_name = os.path.join(ws,mxd[:-4]) + ".pdf" print pdf_name arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
I tried that but reieved the following error message:
Runtime error
Traceback (most recent call last):
File "<string>", line 7, in <module>
TypeError: 'NoneType' object is not iterable
That would mean that there are no map documents in the filepath you gave.
Also:
arcpy.env.workspace = ws = "C:\Users\00177153\Desktop\ArcPy"
Should just be:
arcpy.env.workspace = "C:\Users\00177153\Desktop\ArcPy"
However, if there are no mxds in that directory, then you will have no items in your list to export, hence the error.
Ian Murrayhe is using ws on line 9 for readability he should do
arcpy.env.workspace = r"C:\Users\00177153\Desktop\ArcPy"
ws = r"C:\Users\00177153\Desktop\ArcPy"
yea missed that
Me too i didn't see it till i reconfigured the code
I tested this code, try this
import arcpy, os arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy" mxd_list = arcpy.ListFiles("*.mxd") for mxd in mxd_list: current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd)) pdf_name = os.path.join(ws,mxd[:-4]) + ".pdf" print pdf_name arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
With much appreciation for you - I got it!
See below code that worked (i.e. inserted 'r' and changed mxd_files to mxd_list).
The original code on the ESRI help link should be adjusted. When I tried 'print pdf_name' I got an empty 'test1.pdf'.
>>> import arcpy, os
...
... arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"
...
... mxd_list = arcpy.ListFiles("*.mxd")
...
... for mxd in mxd_list:
...
... current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
... pdf_name = mxd[:-4] + ".pdf"
... arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
...
... del mxd_list
My next challenge will be to export the PDFs to another location than the MXDs. I will put in another question for that. Thank you very much.
See the adjusted code below
>>> import arcpy, os
...
... arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"
... pdfws = r"some\other\path
... mxd_list = arcpy.ListFiles("*.mxd")
...
... for mxd in mxd_list:
...
... current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
... pdf_name = os.path.join(pdfws,mxd[:-4]) + ".pdf"
... arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
...
... del mxd_list