I am trying to create an external python program that, as a scheduled, automated job, could reach multiple ArcPRO projects in one folder and export their layouts to another folder. I haven't been able to find a satisfactory answer online, so I'm trying from scratch right now, I'm at:
import arcpy, os, sys
from arcpy import env
#Set workspace directory
workspace = r"\\ProjectsFolder"
env.workspace = workspace
#Output Location
PDFoutputLoc = r"\\OutputFolder"
#Open/reach into project
commall = workspace.listLayouts()
for layout in commall:
pdf_out = pdf_output + layout.name + ".pdf"
layout.exportToPDF(pdf_out)
#Export all layouts to PDF
#Close and repeat
and the error I'm hitting my head on a wall right now is:
line 17, in <module>
commall = workspace.listLayouts()
AttributeError: 'str' object has no attribute 'listLayouts'
What I think I'm interpreting from this is that it is saying the Layouts are not something that can't be listed. But that doesn't make sense.
to provide line numbers and proper formatting
Code formatting ... the Community Version - Esri Community
and it is the project class that uses the listLayouts method
ArcGISProject—ArcGIS Pro | Documentation
You might also want to check lines 9 & 15.
If I'm following the intent of your code, it looks like your folder name was originally in the "PDFoutputLoc" variable, but in line 15, you're looking for a "pdf_output" variable that doesn't exist.
This seems like the sort of fun problem I enjoy coding, so here's a shot at it. Caveat, I'm rusty on a couple of these calls, and don't have the opportunity to test it right this second, so grain of salt:
import arcpy, os, pathlib
project_folder = r'D:\PROJ'
# Your directory that contains project files goes here.
# Also, if you're doing a raw string with r'', you shouldn't
# need to double-escape your backslash unless you actually
# WANT two backslashes. Also, this could be run on a folder
# that contains multiple subfolders each containing a project
# file, if you so desire. os.walk will go all the way down
# the rabbit hole looking for APRX files.
for dirpath, dirnames, filenames in os.walk(project_folder):
print(dirpath)
for filename in filenames:
# If it's not an APRX file, then we don't care, and can skip it.
if pathlib.Path(filename).suffix != '.aprx':
continue
# Pull the filename and its location as your mp object
print(f'Found Project: {filename}')
project_object = arcpy.mp.ArcGISProject(os.path.join(dirpath, filename))
for layout in project_object.listLayouts():
print(f' Found Layout: {layout.name}')
# Get the name from the Project Name (minus .aprx),
# the Layout Name, and of course a .pdf extension
pdf_name = os.path.join(pathlib.Path(filename).stem, layout.name, '.pdf')
layout.exportToPDF(pdf_name)
print('Complete.') # Nearly all IDEs will be clear when the script is finished,
# but I still do this out of habit for the handful that aren't.
The print commands aren't necessary, of course, but they'll let you keep track of what your script has found and where it is in the process.
I think this is pointing me in the right direction. The code managed to reach into the first project folder, find the .aprx, and find the first layout within it. On the last for loop though it gives me an OSError
Traceback (most recent call last):
File "W:\Websites\GISHomepage\MapGallery\ProProjects\ExportPrototype.py", line 21, in <module>
layout.exportToPDF(pdf_name)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py", line 191, in fn_
return fn(*args, **kw)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 2567, in exportToPDF
return convertArcObjectToPythonObject(self._arc_object.exportToPDF(*gp_fixargs((out_pdf, resolution, image_quality,
OSError: 6MileFireInsurance_Large\6MileFireInsurance_All_Large\.pdf
Is it treating the .pdf as something within the Layout?
The variable workspace is a string and you are trying to call listLayouts() method on a string object. Correct me if I am wrong but listLayouts() method is a method of the arcpy.mp.ArcGISProject. Maybe try this:
project_path = os.path.join(workspace, "projectname.aprx") # Name of the project that you have created
aprx = arcpy.mp.ArcGISProject(project_path)
pdf_output_loc = '\\OutputFolder'
for layout in aprx.listLayouts():
pdf_out = os.path.join(pdf_output_loc, layout.name + ".pdf")
layout.exportToPDF(pdf_out)
del aprx
Not sure if it will work but maybe give it a try?