ExporttoPNG Python for MapSeries

1425
11
02-25-2019 09:10 AM
ElizabethAyarbe
New Contributor II

I'm trying to export my ArcGIS Pro map series to PNG.  I'm using the MapSeries example code #2 from Esri (http://pro.arcgis.com/en/pro-app/arcpy/mapping/mapseries-class.htm ).  I keep getting an Invalid page number error. 

Any help is much appreciated!!

See below code and error:

Code:

import arcpy, os, sys
relpath = "Z:\\41708.02 VAGuidehouse - MAHSO Call 1\\300TIG\\315Maps\\Maps_DDF"

aprx = arcpy.mp.ArcGISProject("CURRENT")
l = aprx.listLayouts()[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
for pageNum in range(1, ms.pageCount + 1):
ms.currentPageNumber = pageNum
print("layoutName{0}".format(ms.pageRow.Market))
pageName = ms.pageRow.Market
l.exportToPNG(relpath + "\\PNG_Test\\S7_MKTOVW_{0}".format(ms.pageRow.Market) + ".png")

Error:

Traceback (most recent call last):
File "<string>", line 10, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\arcobjects\_base.py", line 109, in _set
return setattr(self._arc_object, attr_name, cval(val))
ValueError: Invalid page number '1'.

 

11 Replies
Kara_Shindle
Occasional Contributor III

Hello: I am also having this issue.  I think it has something to do with my Page Numbers being a combination of letters and numbers.  Were you able to resolve this issue?

0 Kudos
ElizabethAyarbe
New Contributor II

Hi!

In Layout Properties>Optional Fields - I had a field selected that was text.  I believe this field needs to be numerical.  I set it to <None> and the above code worked.

Liz

TomBole
Esri Regular Contributor

Hi Elizabeth, 

Map Series page numbers can be alphanumeric. Are you able to export pages using the page number field you want using the UI (i.e. not through ArcPy)?  What kind of values do you have for field you want to use as the page number?  Do you have any characters that are not "standard" alphabet or numbers?

Thanks, 

Tom

0 Kudos
InnovaAdministrator
New Contributor II

Tom:  Sorry for my late response.  At the time I posted I didn't need a page number so changing it to <none> was no big deal.  I now however would like to include text at the end of my map name to describe it better but not the same field as I use in the "Name Field." Can I add text as the page name?  I would like to add "15-f-West-589" where the last three numbers change.  I created a text field in my index layer named "MapPageName" that includes what I would like to name my exports.

0 Kudos
TomBole
Esri Regular Contributor

You can set "MapPageName" as the Name Field for the map series and then export the pages as Multiple PDF Files (page names).

Or you can use ArcPy.mp to export files with the names you specify. You can use the values or any fields in the index layer in which to contruct file names. See map series example 2 in https://pro.arcgis.com/en/pro-app/arcpy/mapping/mapseries-class.htm

Tom

0 Kudos
KansasDOT
New Contributor II

I'm having the same error as Elizabeth.  How do I fix it?  Trying what she suggested of removing fields from the layout options didn't work.

 

import arcpy, os, sys
relpath = "Z:\\Cart\\Users\\elaine\\Aviation_Maps\\Airport Directory\\Airport Directory"
p = arcpy.mp.ArcGISProject("CURRENT")
l = p.listLayouts()[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
for pageNum in range(1, ms.pageCount + 1):
ms.currentPageNumber = pageNum
print("Exporting {0}".format(ms.pageRow.name))
pageName = ms.pageRow.name
l.exportToPNG(os.path.join(relpath, "PNG", f"Ex2_{ms.pageRow.name}.png"))
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 109, in _set
return setattr(self._arc_object, attr_name, cval(val))
ValueError: Invalid page number '1'.

0 Kudos
Kara_Shindle
Occasional Contributor III

Elizabeth: I did get the page number section to work based on your comments, so thank you!  I'm actually now trying to export a range based off of the same ESRI sample.  I keep getting the error on accessing the row attribute, where it says 

ValueError: Type names and field names must be valid identifiers: 'Shape.STArea()'

frustrating!
0 Kudos
JeffBarrette
Esri Regular Contributor

Hello Elizabeth,

I'm sorry for your frustrations.  Can you please provide the line of code that specifies the range?  Again, I think this may have to do with the field data types.  The Page_Range_String parameter does take a string as designed because we want you to be able to export ranges like "1, 3, 5-12" but the exporter needs numbers, not strings. Something like "i, iii, v-vii" can't be supported for a variety of reasons.

One work-around, if non numeric characters are necessary for the dynamic text you want to display, is to create another numeric field in your index feature class that has the page order they way you need it in numeric order.

FYI - I ran example 2 on my machine and it worked fine.  I then changed the page number field to one called "RomanNumerals" and reproduced your original issue.  I'm not familiar with how to reproduce your second ValueError.  You are welcome to send me your code / a project package to jbarrette@esri.com.

Jeff (arcpy.mp team)

0 Kudos
Kara_Shindle
Occasional Contributor III

Jeff:

I'm not sure if you're responding to me or Elizabeth.  I created a similiar post to her's here https://community.esri.com/thread/234564-exportingtopdf-python-for-map-series

Thanks,

0 Kudos