Export APRX files to PDF

1219
3
01-25-2019 02:47 PM
JaredPilbeam2
MVP Regular Contributor

I'm not sure why this isn't working correctly. I want to export the layouts of the APRX files I have in a folder as PDFs and save them in the same place. It will run with no errors but there is no PDF saved in the outfolder. There is, however, one PDF saved one place back in the path. 

import arcpy, os

rootDir = r"path\to\TestFolder"
outfolder = r"path\to\TestFolder"

for dirName, subdirList, fileList in os.walk(rootDir):
    for file in fileList:
        if file.endswith(".aprx"):
            current_aprx = arcpy.mp.ArcGISProject(os.path.join(rootDir, file))
            pdf_name = os.path.join(outfolder, file[:-4])+".pdf"
            for lyts in current_aprx.listLayouts():
                lyts.exportToPDF(outfolder)
                print("exporting APRX to PDF: {}".format(pdf_name))

del file‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's the print statements in the Interpreter window:

exporting APRX to PDF: path\to\TestFolder2\MUNICIPAL_LIMITS_A - Copy..pdf
exporting APRX to PDF: path\to\TestFolder2\MUNICIPAL_LIMITS_A..pdf
Tags (3)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
pdf_name = os.path.join(outfolder, file[:-4])
for cnt, lyt in enumerate(current_aprx.listLayouts()):
    lyt.exportToPDF(outfolder)
    print("exporting APRX to PDF: {}{}{}".format(pdf_name, cnt, ".pdf")‍‍‍‍‍‍‍‍)‍‍‍‍

use enumerate and move the add the file extension at the end

JaredPilbeam2
MVP Regular Contributor

Thanks Dan. 

No errors, but still not seeing PDFs saved to the outFolder. There are two APRXs in this test folder. And as before running this script saves one PDF not in the TestFolder2 folder, but one spot back. And the PDF is named "TestFolder2". Here's the whole thing again with the print statements below it.

import arcpy, os

rootDir = r"path\to\TestFolder2"
outfolder = r"path\to\TestFolder2"

for dirName, subdirList, fileList in os.walk(rootDir):
    for file in fileList:
        if file.endswith(".aprx"):
            current_aprx = arcpy.mp.ArcGISProject(os.path.join(rootDir, file))

            pdf_name = os.path.join(outfolder, file[:-4])
            for cnt, lyt in enumerate(current_aprx.listLayouts()):
                print(lyt.name)
                lyt.exportToPDF(outfolder)
                print("exporting APRX to PDF: {}{}{}".format(pdf_name, cnt, ".pdf"))

current_aprx.save()
del current_aprx
MUNICIPAL_LIMITS_A
exporting APRX to PDF: path\to\TestFolder2\MUNICIPAL_LIMITS_A - Copy.0.pdf
MUNICIPAL_LIMITS_A
exporting APRX to PDF: path\to\TestFolder2\MUNICIPAL_LIMITS_A.0.pdf
0 Kudos
JaredPilbeam2
MVP Regular Contributor

With additional help I was able to run the code with the desired output.

Whereas before all PDFs except the last one were overwritten because they shared the same name as the folder (TestFolder2), they are now given a unique name in the exportToPDF() function.

import arcpy, os
rootDir = r"path\to\TestFolder2"
outfolder = r"path\to\TestFolder2"
for dirName, subdirList, fileList in os.walk(rootDir):
    for file in fileList:
        if file.endswith(".aprx"):
            current_aprx = arcpy.mp.ArcGISProject(os.path.join(rootDir, file))
            for cnt, lyt in enumerate(current_aprx.listLayouts()):
                pdf_name = os.path.join(outfolder, file[-5], lyt.name)
                print(lyt.name)
                lyt.exportToPDF(outfolder + "/" + lyt.name)
                print("exporting APRX to PDF: {}{}{}".format(pdf_name, cnt, ".pdf"))

current_aprx.save()
del current_aprx
0 Kudos