Export Map Series using Python

202
1
03-20-2020 09:35 AM
PaulSweeney3
Occasional Contributor II

i have developed a model to download a layer from arcgis online every week and overwrite the GDB with the previous weeks information. I was then manually feeding this into a project and exporting a Map series for a weekly report. i decided to also try and automate this process so i could have reports generated automatically every week. i am a beginner at python and am self thought so please bear with me. 

my script is as follows which i developed in a notebook 

#!/usr/bin/env python
# coding: utf-8

# In[98]:


import arcpy
import os
import sys
aprx = arcpy.mp.ArcGISProject("CURRENT")
default = aprx.defaultGeodatabase
default


# In[116]:


import os
directory = 'C:\\Usersxxxxxxxxxxxxxx'

for filename in os.listdir(directory):
    if filename.endswith(".gdb"):
        print(directory+filename)
        newGDB = directory+filename
        newGDB
        
        


# In[117]:


newGDB


# In[101]:


aprx = arcpy.mp.ArcGISProject(r"C:\Users\xxxxxxxxxxx.aprx")
default = aprx.defaultGeodatabase
default


# In[60]:


import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Users\xxxxxxx.aprx")
m = aprx.listMaps("Map")[0]
lyr = m.listLayers("Assignments")[0]

lyr.connectionProperties


# In[102]:


newCP={'dataset': 'Assignments',
 'workspace_factory': 'File Geodatabase',
 'connection_info': {'database': newGDB}}
newCP


# In[103]:


lyr.updateConnectionProperties(lyr.connectionProperties, newCP)


# In[104]:


lyr.connectionProperties


# In[105]:


tableAtt=m.listTables("Assignments__ATTACH")[0]
tableAtt.connectionProperties


# In[106]:


tableAtt.updateConnectionProperties(tableAtt.connectionProperties, newCP)


# In[107]:


tableAtt.connectionProperties


# In[108]:


tableGDBService=m.listTables("GDB_ServiceItems")[0]
tableGDBService.connectionProperties


# In[109]:


tableGDBService.updateConnectionProperties(tableGDBService.connectionProperties, newCP)


# In[110]:


tableGDBService.connectionProperties


# In[111]:


aprx.save()


# In[112]:


l = aprx.listLayouts()[0]

l


# In[113]:


import os
import sys
import datetime
from datetime import datetime
relpath = os.path.dirname(sys.argv[0])
printpath = r"T:\xxxxxxxxxxxxx"


str=current_time = datetime.today().strftime('%d-%m-%Y') 

relpath











# In[114]:


aprx = arcpy.mp.ArcGISProject(r"xxxxxxxxxxx.aprx")
l = aprx.listLayouts()[0]

if not l.mapSeries is None:
    ms = l.mapSeries
    if ms.enabled:
        ms = l.mapSeries
        pagecount = ms.pageCount
        indexLyr = ms.indexLayer
        
        
pagecount


# In[115]:


ms.exportToPDF(printpath+str,embed_fonts = True,resolution=400,georef_info=False,image_quality='FASTER',page_range_type = 'ALL',jpeg_compression_quality=25,output_as_image=False,layers_attributes='NONE',image_compression='ADAPTIVE')


# In[ ]:







it had work when i run it through the notebook but when i run it in a model in order to schedule it i am getting the following error. 

Traceback (most recent call last):
File "C:\Users\xxxxxxxxxxxxxxxxx.tbx#ReplaceDSandPDF_ExcavationReports.py", line 50, in <module>
IndexError: list index out of range
Failed to execute (Replace DS and PDF).

Any tips on where i am going wrong or even how to improve on my process are greatly appreciated. 

0 Kudos
1 Reply
DanPatterson_Retired
MVP Esteemed Contributor
m.listLayers("Assignments")[0]

Which means m.listLayers("Assignments") got nothing... then you tried to take the first slice of it, which will yield the error, so check you 'm' and make sure it has "Assignments" layers (at least one)