Select to view content in your preferred language

Help with looping

1113
3
Jump to solution
08-29-2012 03:12 PM
SeanConlon
Occasional Contributor
Hello,      I'm new to arcmapping and modelbuilder. My first task was create a model (fig 1) to run thru a .gdb, check for a certain field, then run the frequency tool, and output the resulting table to a different .gdb. It works exactly as I want it. Now I want to take each those tables (about 50 of them) and create a report (.rlf) and export it to PDF. The code below, which I've loaded as a script into a toolbox, works correctly but only one table at a time. How can I (1) loop it thru each table? and (2) not overwrite the previous PDF?
Ultimately I want to insert it to the first model ( Fig 1)

Any help or suggestions would be greatly appreciated

import arcpy  cttable=arcpy.mapping.TableView(r"C:\Workspace\CaseTypeTables.gdb\AGR_SET_RECON_LN_case_types") arcpy.mapping.ExportReport(cttable,r"C:\Workspace\working\LAS_case-type_report.rlf",r"C:\Workspace\working\successPDFreport.pdf") del cttable
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SeanConlon
Occasional Contributor
hi Jake  thanks for replying so quickly!!

It works!!!!!! and not only that I was able to plug it into that original model ( fig 1, from above ) and it works exactly that it's supposed to.
I did have to tweak one thing - deleting the "+os.sep" from line 10. I don't know why it works without it but it does.
below is the current working version, with comments. I know its not the way your supposed to do it but it helps this novice understand whats going on.
some questions though
1. what does the * do?
2. what does the get message () do?
# import arcpy modules and geoprocessing tools import arcpy, os # not sure what this does from arcpy import env # enables the output to overwrite anything that was there before env.overwriteOutput = 1 # not sure what this does  env.workspace = r"C:\Workspace\CaseTypeTables.gdb"  #1sttables is a variable that contains a list of all the tables from casetypetables.gdb #what is the * for  lstTables = arcpy.ListTables("*")   #loop to run thru all the tables in 1sttables and execute the exportreport function from arcpy #table is a variable for a single table from 1sttable #cttable is a variable calling the arcpy funtion tableview to view the single table #the TRY  command is executing the exportreport function on cttable using the report format from # LAS_case-type_report.rlf and exporting the resulting PDF at C:\workspace\. . . . for table in lstTables:     cttable = arcpy.mapping.TableView(table)     try:         arcpy.mapping.ExportReport(cttable, r"C:\Workspace\working\LAS_case-type_report.rlf", r"C:\Workspace\working\_"+table+".pdf")     except:         arcpy.GetMessages()

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Sean,

Try the following:

import arcpy, os
from arcpy import env
env.overwriteOutput = 1
env.workspace = r"C:\Workspace\CaseTypeTables.gdb"

lstTables = arcpy.ListTables("*")
for table in lstTables:
    cttable = arcpy.mapping.TableView(table)
    try:
        arcpy.mapping.ExportReport(cttable, r"C:\Workspace\working\LAS_case-type_report.rlf", r"C:\Workspace\working\successPDFreport.pdf" + os.sep + table + "_PDFreport.pdf")
    except:
        arcpy.GetMessages()
0 Kudos
SeanConlon
Occasional Contributor
hi Jake  thanks for replying so quickly!!

It works!!!!!! and not only that I was able to plug it into that original model ( fig 1, from above ) and it works exactly that it's supposed to.
I did have to tweak one thing - deleting the "+os.sep" from line 10. I don't know why it works without it but it does.
below is the current working version, with comments. I know its not the way your supposed to do it but it helps this novice understand whats going on.
some questions though
1. what does the * do?
2. what does the get message () do?
# import arcpy modules and geoprocessing tools import arcpy, os # not sure what this does from arcpy import env # enables the output to overwrite anything that was there before env.overwriteOutput = 1 # not sure what this does  env.workspace = r"C:\Workspace\CaseTypeTables.gdb"  #1sttables is a variable that contains a list of all the tables from casetypetables.gdb #what is the * for  lstTables = arcpy.ListTables("*")   #loop to run thru all the tables in 1sttables and execute the exportreport function from arcpy #table is a variable for a single table from 1sttable #cttable is a variable calling the arcpy funtion tableview to view the single table #the TRY  command is executing the exportreport function on cttable using the report format from # LAS_case-type_report.rlf and exporting the resulting PDF at C:\workspace\. . . . for table in lstTables:     cttable = arcpy.mapping.TableView(table)     try:         arcpy.mapping.ExportReport(cttable, r"C:\Workspace\working\LAS_case-type_report.rlf", r"C:\Workspace\working\_"+table+".pdf")     except:         arcpy.GetMessages()
0 Kudos
JakeSkinner
Esri Esteemed Contributor
1. what does the * do?
-This is a wildcard.  Specifying this tell the function to list all tables within your geodatabase.

2. what does the get message () do?
-This reports any errors/warnings that may occur when you try to execute a command.  For example, if the 'arcpy.mapping.ExportReport' function failed, it would output the error messages.  More information can be found here.
0 Kudos