Exporting layout file from ArcGIS Pro project to *.pagx file using ArcPy?

1391
3
08-12-2019 05:58 AM
NikolayGolosov
New Contributor

I'm trying to implement a tool to import *.pagx files exported from ArcGIS Pro to a project, modify text elements and export the altered layouts back to the *.pagx files. It is pretty easy to export these files via ArcGIS Pro GUI, but I'm struggling to find a way to export these files via ArcPy.

So far I used the following code to modify layout from the individual *.pagx file:

import arcpy
layout = arcpy.mp.ConvertLayoutFileToLayout(r"C:\temp\templates\8x11 Landscape.pagx")
for element in layout.listElements('TEXT_ELEMENT'):
 if element.name == 'Text 4':
 element.text = "New text"

but there is no way to save the modified layout object as a file. Also I tried to loop through the layouts, imported to the Pro project

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Users\username\Documents\ArcGIS\Projects\Layouts\Layouts.aprx")
for layout in aprx.listLayouts():
 for element in layout.listElements('TEXT_ELEMENT'):
 if element.name == 'Text 4':
 element.text = "New text"
aprx.save()

Using the save() method I can save the project, but again there is no way to export the *.pagx files. I heard somewhere that I could export these *.pagx files via the .Net API only, but I'm still not 100% sure if such export feature is not implemented in arcpy, or I overlooked something. 

0 Kudos
3 Replies
NikolayGolosov
New Contributor

Tried to employ jsontocim module, located in "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\cim\cimloader\jsontocim.py"

from arcpy.mp import ConvertLayoutFileToLayout
from arcpy.cim.cimloader.cimtojson import CimJsonEncoder
from json import dumps

layout = ConvertLayoutFileToLayout(r"C:\temp\templates\8x11 Landscape.pagx")
cim = layout.getDefinition('V2')
outJSON = dumps(cim, cls=CimJsonEncoder, indent=2)
print(outJSON)

It produced the output, resembling the structure of the *.pagx file, exported via GUI, but some parts are missing, and some of them have a different structure, so I were unable to import this file back to ArcGIS Pro. I'd be glad if anyone advice if there is any way to use that approach(with CimJsonEncoder) to receive a valid pagx JSON file?

0 Kudos
NikolayGolosov
New Contributor

Any thoughts on that?

0 Kudos
GeoNZ
by
Occasional Contributor

I'm also needing to do this.

What's needed is a method to make a copy of a existing layout/ 'save as' is required to make the most of automated map production.

For example, I have a tool which takes an input layout in a project and updates it (i.e. the extent, layout text elements etc). This is considered the 'parent' map.

There may be multiple 'children' of that 'parent' and I want the second tool to save a copy of the 'parent' layout (including any information that has been added in the first tool), then manipulate it and save it/add it to the current project.

So, something like a 

for layout in aprx.listLayouts("Layout Parent")[0]:
   layout.saveAs('pathtoChild1.pagx', "Updated Layout name")

   
0 Kudos