Select to view content in your preferred language

Help - Exporting Multiple Map Documents to PDF using Python

4823
13
Jump to solution
08-06-2015 08:06 AM
anayoung
Regular Contributor

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

0 Kudos
13 Replies
anayoung
Regular Contributor

Thank you, Wes.

That worked very well except that I had to leave off the "del mxd_list" this time. Doing that the code created another empty "test1" PDF along with the other ones I wanted. Any thoughts as to why leaving off the "del mxd_list" and the empty "test1" PDF happened?

0 Kudos
WesMiller
Deactivated User

Not sure unless you ran it one time and it failed, then the "test1" may have been still in the list. I would try closing python completely down then reopening and adding back the "del mxd_list" to see if it clears it up you may also want ti delete the already created set or add to the top of your code under the imports

anayoung
Regular Contributor

Okay - it worked after I closed it down a few times and added the 'del mxd_list' back in.

Whew! Thanks again, Wes.

0 Kudos
anayoung
Regular Contributor

Thanks, Rebecca. That worked out well. Your help was much appreciated by this beginner.

0 Kudos