Hi everyone!
I want to preface this by saying I am VERY new to coding so I could be doing this totally wrong. Also, please let me know if I'm posting this question in the wrong board.
In ArcGIS Pro, I wanted to be able to export a single PDF file consisting of Spatial Map Series Pages. However, I needed it to export two different layouts, alternating between layouts. For example, it would look like: FirstLayout Page 1, SecondLayout Page 1, FirstLayout Page 2, SecondLayout Page 2, FirstLayout Page 3, SecondLayout 3.... and so on.
The code I wrote works, but I have a few questions because I want to simplify it and somehow create a tool out of it so I don't have to input the code every time.
Here's the code with questions:
# coding: utf-8
# This code groups FirstLayout with SecondLayout and exports in one PDF file.
#FIRST QUESTION: do all of these need to be imported for this specific task? Does it even matter? I am still confused about the whole third-party module concept.
import arcpy
import os
import sys
# Set the output PDF file path
output_pdf = r'C:\Users\BlahBlah\FilePath\Code Testing\Test.pdf'
#SECOND QUESTION: Is there a way to have the code auto fill layout names?
Currently, I have to replace layout names every time I change it.
# List of layout names
layout_names = ['FirstLayout', 'SecondLayout']
# Reference the active ArcGIS Pro Project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Create the PDF document
pdf_document = arcpy.mp.PDFDocumentCreate(output_pdf)
#THIRD QUESTION: Here is where I get messed up. I should be able to reference the map series below, under "# Iterate through the unique IDs" BUT if I don't reference the map series before that step, I get this error message:
"<string>", line 1, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get
return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))
RuntimeError: Unexpected error."-- Why??
#Reference the map series
for layout_name in layout_names:
layout = aprx.listLayouts(layout_name)[0]
map_series = layout.mapSeries
# Iterate through the unique IDs
for unique_id in range(1, map_series.pageCount + 1):
for layout_name in layout_names:
layout = aprx.listLayouts(layout_name)[0]
map_series = layout.mapSeries
map_series.currentPageNumber = unique_id
pageName = map_series.pageRow.UNI_IDS__LO_ # UniqueID is the attribute field name used to define the map series. Change accordingly if a different field name is used.
temp_pdf = r'C:\Users\BlahBlah\FilePath\Code Testing\TempLayout.pdf'
layout.exportToPDF(temp_pdf)
pdf_document.appendPages(temp_pdf)
#LAST QUESTION: Does this always have to be loaded after the code is done?
# After the software finishes the PDF, enter this.
pdf_document.saveAndClose()