Hello, I am a newbie ArcPy/Python user and am struggling with getting a mapseries to export with Python code. I would like to have the code open the APRX, select from the index layer dates that have fallen in the last 2 days, then run the ms.exportToPDF on the selected. I am using Python 3.9.11 and Arcpro 3.0
Here is the code I have (i have some print functions splattered throughout for data/variable checking):
import arcpy, os, sys
relpath = os.path.dirname(sys.argv[0])
print(relpath)
from datetime import date, timedelta
today = date.today()
print(today)
date2 = today - timedelta(days=2)
date_time = date2.strftime("%m/%d/%Y")
print(date2)
aprx = arcpy.mp.ArcGISProject(r"W:\1.GIS\TEMP\AutoExports\PreSites_bu.aprx")
l = aprx.listLayouts()[0]
print ("Printing map series for layout: {}".format(l.name))
sqlquery = '"' + "EditDate" + '"' + " >= " + date_time
print(sqlquery)
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms = l.mapSeries
indexLyr = ms.indexLayer
# arcpy.ca.SelectLayerByDateAndTime(indexLyr, "NEW_SELECTION", "SINGLE_TIME_FIELD", "EditDate", None, None, "DATE", "RECENCY", "2 Days")
arcpy.SelectLayerByAttribute_management(indexLyr, "NEW_SELECTION", sqlquery)
ms.exportToPDF("W:\1.GIS\TEMP\Presite.pdf", "SELECTED")
I have tried using the REM'd out selection using recency, and also the active selection using the built "sqlquery". I am getting this error:
Traceback (most recent call last):
File "W:\BCHydro\1.GIS\TEMP\AutoExports\BCH_AutoExportPresite.py", line 30, in <module>
arcpy.SelectLayerByAttribute_management(indexLyr, "NEW_SELECTION", sqlquery)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10780, in SelectLayerByAttribute
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 10777, in SelectLayerByAttribute
retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Solved! Go to Solution.
Hi @LeonConsus
If you open up your APRX and use the SelectByAttributes tool and add a query on the date field, check the SQL button (highlighted in yellow) and you will see the format that the SQL query needs to be in for a date field. Similar to below.
Try set your sqlquery to
sqlquery = "EditedDate >= timestamp '{0} 00:00:00'".format(date_time)
Hi @LeonConsus
If you open up your APRX and use the SelectByAttributes tool and add a query on the date field, check the SQL button (highlighted in yellow) and you will see the format that the SQL query needs to be in for a date field. Similar to below.
Try set your sqlquery to
sqlquery = "EditedDate >= timestamp '{0} 00:00:00'".format(date_time)
Hi Clubdebambos!
That was a partial solution! And once I got that figured out, my other issues became more apparent and I was able to get a result! Apparently Py does not like \1 in the directory structure either, so I corrected that and everything is now running!
Thanks!