I'm trying to automate exporting multiple layouts from a project. I want all the exported files to share the same name as the project, with an appended string. The problem is I can't find a simple way to extract the project name. I know it's included in the filePath property of the ArcGISProject object, but is there an easier way than having to extract the name from the full path? I know I could use the project name from metadata, but I don't usually fill that out and I'd rather not fill it out simply for this purpose. I really just want to use the name as found in the project settings:
I'm able to use that as dynamic text in the layout; would be great to find it with ArcPy, too.
Solved! Go to Solution.
ArcGISProject—ArcPy | Documentation
filePath (Read Only) | Returns a string value that reports the fully qualified project path and file name. |
How, and where, are you running your script? That might have some influence on the easiest way to proceed.
The full path would be useful since you can also extract folder information beyond the aprx as well.
Python parsing is simple
junk = r"C:\Git_Dan\npgeom\Project_npg\npgeom\junk.aprx"
junk.split("\\")[-1][:-5] # ---- without extension
'junk'
junk.split("\\")[-1] # ---- with extension
'junk.aprx'
ArcGISProject—ArcPy | Documentation
filePath (Read Only) | Returns a string value that reports the fully qualified project path and file name. |
How, and where, are you running your script? That might have some influence on the easiest way to proceed.
The full path would be useful since you can also extract folder information beyond the aprx as well.
Python parsing is simple
junk = r"C:\Git_Dan\npgeom\Project_npg\npgeom\junk.aprx"
junk.split("\\")[-1][:-5] # ---- without extension
'junk'
junk.split("\\")[-1] # ---- with extension
'junk.aprx'
Thank you, Dan. It looks like extracting the name from the filePath is the only (but easy) way. I was trying to use os.path.join at first, but then I realized it's unnecessary. Essentially the same as your suggestion, I created a variable for filePath with ".aprx" hacked off (without any string splitting; that would be good for isolating the name, but I want the whole string now, eliminating os.path.join). Then in exportToPDF, I called that variable and appended the string I want for the exported file.
I'm using this script interactively, one project at a time. For now, at least, I have no plan to run on multiple projects at once.
import arcpy
## define variables for project and layouts
aprx = arcpy.mp.ArcGISProject("CURRENT")
path = aprx.filePath[:-5]
img = aprx.listLayouts("Aerial Imagery")[0]
luz = aprx.listLayouts("Land Use & Zoning")[0]
gsm = aprx.listLayouts("Growth Strategies")[0]
sap = aprx.listLayouts("Small Area Plan")[0]
vic = aprx.listLayouts("Vicinity")[0]
## export layouts to PDF or PNG as appropriate
img.exportToPDF(path+"_img.pdf")
luz.exportToPDF(path+"_luz.pdf")
gsm.exportToPDF(path+"_gsm.pdf")
sap.exportToPDF(path+"_sap.pdf")
vic.exportToPNG(path+"_vic.png",150)