0

I have ArcMap 10.5 installed in my PC and I'm using ArcPy to updated different MXDs and then export PDF.

This one is a very simple and plain script, but I can't figure out why it's not exporting the PDF.

It returns the following message:

Traceback (most recent call last):
  File "c:/Users/user/Desktop/automatelayouts.py", line 71, in <module>
    arcpy.mapping.ExportToPDF(mxd_copy_path_obj, pdf_out_path,resolution=150)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\mapping.py", line 1156, in ExportToPDF
    layout.exportToPDF(*args)
AttributeError: PageLayoutObject: Error in executing ExportToPDF

Here is my script:

Script:

import arcpy
import os

area_path=os.environ['USERPROFILE']

run_number = "1"
parameter_folder=os.path.join(area_path,r"Desktop\test\input\parameters")
input_mxd_folder=os.path.join(area_path,r"Desktop\test\input\mxd")
lyrfolder=os.path.join(area_path,r"Desktop\test\input\lyr")
output_mxd_folder= os.path.join(area_path,r"Desktop\test\output\mxd\run"+run_number)
if not os.path.isdir(output_mxd_folder): os.makedirs(output_mxd_folder)
output_pdf_folder= os.path.join(area_path,r"Desktop\test\output\pdf\run"+run_number)
if not os.path.isdir(output_pdf_folder): os.makedirs(output_pdf_folder)

title="Krigging Map  -  Aluminum"
metal="Aluminum"

input_parameter_shp=os.path.join(parameter_folder,"Aluminium_area.shp")
mxd_template_list=["report1.mxd","report2.mxd","report3.mxd"]
fig=0
for mxd_template_name in mxd_template_list:
    fig+=1
    print("creating figure_"+str(fig))
    pdf_name=mxd_template_name.replace(".mxd","_"+metal+".pdf")
    pdf_out_path = os.path.join(output_pdf_folder,pdf_name)
    if not os.path.isfile( pdf_out_path ):
        mxd_template_path=os.path.join(input_mxd_folder,(mxd_template_name))
        mxd_copy_name=mxd_template_name.replace(".mxd","_"+metal+".mxd")
        print("--mxd_copy_name: "+mxd_copy_name)
        mxd_copy_path=os.path.join(output_mxd_folder,mxd_copy_name)

        print("creating the map document for the template mxd...")
        mxd_template_path_obj = arcpy.mapping.MapDocument(mxd_template_path)
        print("making a copy from template mxd...")
        mxd_template_path_obj.saveACopy(mxd_copy_path)
        print("Deleting template MXD to free up memory...")
        del mxd_template_path_obj

        mxd_copy_path = os.path.join(output_mxd_folder,mxd_copy_name)

        print("creating a new map document for the copied mxd...")
        mxd_copy_path_obj = arcpy.mapping.MapDocument(mxd_copy_path)
        
        print("finding the metal layer...")
        lyr=arcpy.mapping.ListLayers(mxd_copy_path_obj,"metal")[0] 

        print("executing arcpy.Describe...")
        desc=arcpy.Describe(input_parameter_shp)
        print("replacing the lyr datasource...")
        lyr.replaceDataSource(desc.path,"SHAPEFILE_WORKSPACE",desc.basename)
        print("updating table of contents...")
        arcpy.RefreshTOC() 

        style_lyr= os.path.join(lyrfolder,"aluminium.lyr")
        print("updating symbology...")
        arcpy.ApplySymbologyFromLayer_management(lyr, style_lyr)
        
        print("updating element name texts...")
        title_text=arcpy.mapping.ListLayoutElements(mxd_copy_path_obj,"TEXT_ELEMENT","titulo")[0]
        title_text.text=title
        figure_number_text=arcpy.mapping.ListLayoutElements(mxd_copy_path_obj,"TEXT_ELEMENT","figura")[0]
        figure_number_text.text="Figura "+str(fig)

        print("saving the copied mxd...")
        mxd_copy_path_obj.save()

        print("exporting PDF...")
        
        arcpy.mapping.ExportToPDF(mxd_copy_path_obj, pdf_out_path,resolution=150)
        print("PDF saved successfully...")
        
        print("Deleting MXD to free up memory...")
        del mxd_copy_path_obj
        
        print("finishing iteration {}...\n\n".format(fig))

print("SCRIPT FINISHED...")
1
  • Your script worked for me...Except I commented out lines 44-62. If the PDF file already exists, is there a lock on it? An easy way to check for a lock on the file is to try and rename it in Windows-Explorer. Sometimes ArcMap holds locks on PDFs. Also, do you have write-access to the output folder location (seems unlikely you would not have access, but worth a shot)?
    – Keggering
    Apr 14, 2021 at 0:49

1 Answer 1

1

I don't see where the script or updatemap class exports to PDF (arcpy.mapping.ExportToPDF). Adding an "exportPDF" method to your class, then calling it in the script worked for me.

    def exportPDF(self, pdfPath):
        """doc string"""
        arcpy.mapping.ExportToPDF(self.mxd, pdfPath)

Unrelatedly, I suggest using os.path.join to build your paths.

For example:

savemxdfolder = os.path.join(workspace, "outmxd") # <-- This
savemxdfolder = workspace+"\outmxd" #<--- Instead of this
2
  • The ExportToPDF function is near the bottom of the script nested into the for/if not section. It is above the 4th from last print statement.
    – Baltok
    Apr 13, 2021 at 21:40
  • It appears to have been added in an edit, in addition to a lot of other script, and the "updatemap" class has been removed.
    – Keggering
    Apr 14, 2021 at 0:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.