Select to view content in your preferred language

ArcGIS Pro Export python

906
5
01-29-2019 10:44 AM
CCWeedcontrol
Regular Contributor

I am trying to export a project to pdf.

With arcpy.mapping i had the following and worked

import arcpy, os, sys 

mxd = arcpy.mapping.MapDocument("CURRENT")
folder = os.path.dirname(mxd.filePath) # H:/GIS_Data
fullfilename = os.path.basename(mxd.filePath) # junk.mxd
filename = os.path.splitext(fullfilename) # [junk, mxd]
name = filename[0] # junk
pdfname = name + '.pdf' # junk.pdf
outpdf = folder + "/" + pdfname # H:/GIS_Data/junk.pdf
arcpy.mapping.ExportToPDF(mxd, pdfname, "PAGE_LAYOUT",0 ,0 ,"210")

I am attempting to do this with ArcGIS Pro arcpy.mp and have the following but having a hard time getting it started.

I get AttributeError: 'str' object has no attribute 'listLayouts' with the following.

import arcpy, os, sys 

p = arcpy.mp.ArcGISProject("CURRENT").filePath
l = p.listLayouts()[0]
l.exportToPDF(R"C:\temp\Ex1_WA.pdf", 'CURRENT',0 ,0 ,"210")
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus
ilePath
(Read Only)

Returns a string value that reports the fully qualified project path and file name.

So it should work unless you are not running it from with a toolbox in an open session of Pro or from Pro's python window.

To test, just throw the actual path in there, because 'CURRENT' doesn't seem to be returning anything other than a string (perhaps None)

0 Kudos
CCWeedcontrol
Regular Contributor

I was trying to run it in Pro's Python window, is this not allowed?

can you not set the os.path.dirname so you won't have to give the full path name?

0 Kudos
DanPatterson_Retired
MVP Emeritus

perhaps you are shortcutting

p = arcpy.mp.ArcGISProject("CURRENT")

pth = p.filePath

the first line, assuming it works, should return a project

the second line returns the path to the project

the way you had it

p = arcpy.mp.ArcGISProject("CURRENT").filePath

just returns the path to the project and not the actual project which is needed later on.

CCWeedcontrol
Regular Contributor

I got it to work the the following, thanks for the help.

import arcpy, os, sys 

p = arcpy.mp.ArcGISProject("CURRENT")
folder = os.path.dirname(p.filePath)
fullfilename = os.path.basename(p.filePath)
filename = os.path.splitext(fullfilename)
name = filename[0]
pdfname = name + '.pdf' 
outpdf = folder + "/" + pdfname
l = p.listLayouts()[0]
l.exportToPDF(pdfname,resolution = 300)
0 Kudos
DanPatterson_Retired
MVP Emeritus

so not getting the aprx object was the issue then.  

with the extra path stuff cut out

0 Kudos