Summary
The ReportSection object references a report section in a report. It provides access to common properties such as a definition query and a method to set the reference data source.
Discussion
An ArcGIS AllSource report can contain multiple report sections. Report sections are accessed using the listSections method on the Report object. This returns a Python list of ReportSection and ReportLayoutSection objects. Each report section can be referenced by a unique name and has a REPORT_SECTION type.
A ReportSection has a data source that can be identified using the referenceDataSource property. To change the data source, use the setReferenceDataSource method. This method maps the fields from the new data source to the fields of the report that were defined by the original data source. The field names, not the field aliases, must be an exact, case-sensitive match.
A list of fields and statistic for a report section can be accessed using the fields and statistics properties. These properties are for the main report section and do not include related reports.
Related reports within report sections also have a source. To change the source, use the setRelatedReportSource method. Like changing the main data source, fields are mapped to the report defined by the original source.
To filter the records exported to PDF, modify the report section's definitionQuery property. To exclude a report section from the exported PDF, set the visible property to false.
Properties
Property | Explanation | Data Type |
definitionQuery (Read and Write) | The report section's definition query. Use this to filter the records exported to PDF. | String |
fields (Read Only) | The fields in the report section, including field name, sort order, and grouping. This does not include related reports. The keys for the dictionary are defined as follows:
The available sorting options are defined as follows:
| Dictionary |
name (Read and Write) | The report section's name. It is important that all report sections in a project have a unique name so they can be easily referenced by name. | String |
referenceDataSource (Read Only) | Returns the report section's reference data source connection information as a Python dictionary. The keys for the dictionary are defined as follows:
| Dictionary |
statistics (Read Only) | The statistics in the report section, including field name and statistic type. This does not include related reports. The keys for the dictionary are defined as follows:
The available statistics for a report are defined as follows:
| Dictionary |
type (Read Only) | The listSections method on the Report object returns both ReportSection and ReportLayoutSection objects. A report section has a REPORT_SECTION type. | String |
visible (Read and Write) | The report section visibility. Set this to False to exclude the section when exporting to PDF. | Boolean |
Method Overview
Method | Explanation |
setReferenceDataSource (data_source) | Sets a report section's reference data source. |
setRelatedReportSource (related_report_data_source, related_report_section_name, relate_or_relationship_class_name) | Sets a related report's data source. |
Methods
setReferenceDataSource (data_source)
setRelatedReportSource (related_report_data_source, related_report_section_name, relate_or_relationship_class_name)
Parameter | Explanation | Data Type |
related_report_data_source | The data source for the new related report. This parameter can be a Layer object, a Table object, or a string that represents the path to an external data source. (The default value is None) | Object |
related_report_section_name | The name of the related report. (The default value is None) | String |
relate_or_relationship_class_name | The name of the new relate or relationship class. (The default value is None) | String |
The setRelatedReportSource method sets the data source that the related report references using a Layer object, a Table object, or a string that represents the path to an external data source, the related report name, and the new relate or relationship class name.
The following script sets a new related report source using an in-map relate:
aprx = arcpy.mp.ArcGISProject('current')
report = aprx.listReports("States Report with Relate")[0]
reportSection = report.listSections()[0]
map = aprx.listMaps("States with Capitals")[0]
newRelatedLayer = map.listLayers("Capitals")[0]
relatedReportName = "cities: Related Report"
newRelateName = "states_capitals"
reportSection.setRelatedReportSource(newRelatedLayer, relatedReportName, newRelateName)
Code sample
The following script accesses all report sections in a report:
aprx = arcpy.mp.ArcGISProject(r"C:\DemoData\NationalParks.aprx")
report = aprx.listReports("National Parks Report")[0]
sections = [s.name for s in report.listSections() if s.type=="REPORT_SECTION"] # Get list of report section names
for s in sections:
print(s)
# National Parks Overview
# Park Highlights
# Annual Review
The following script sets a report section visibility to False:
aprx = arcpy.mp.ArcGISProject(r"C:\DemoData\NationalParks.aprx")
report = aprx.listReports("National Parks Report")[0]
section = report.listSections("National Parks Overview")[0] # Find the report section by name
section.visible = False
The following script sets the reference data source for the second report section in a report to a table in a map:
aprx = arcpy.mp.ArcGISProject(r"C:\DemoData\NationalParks.aprx")
map = aprx.listMaps("National Parks")[0] # Find a map in the project
table = map.listTables()[0] # Find the first table in the map
report = aprx.listReports("National Parks Report")[0]
section = report.listSections()[1] # Find the report section by index
section.setReferenceDataSource(table)
The following script modifies the definition query for the third report section in a report:
aprx = arcpy.mp.ArcGISProject(r"C:\DemoData\NationalParks.aprx")
report = aprx.listReports("National Parks Report")[0]
section = report.listSections("Annual Review")[0] # Find the report section by name
section.definitionQuery = "ParkName = 'Yosemite'"
The following script gets the fields and statistics for a report section:
aprx = arcpy.mp.ArcGISProject(r'C:\temp\Parcels.aprx') # Find the project
report = aprx.listReports()[1] # Find the second report by index
report_section = report.listSections()[0] # Find the first report section
print(report_section.fields) # Get fields
# [{'fieldName': 'ASR_LANDUSE', 'sortInfo': 'ASC', 'groupField': True}, {'fieldName': 'APN_8', 'sortInfo': 'NONE', 'groupField': False}, {'fieldName': 'ACREAGE', 'sortInfo': 'NONE', 'groupField': False}]
print(report_section.statistics) # Get statistics
# [{'fieldName': 'APN_8', 'statistic': 'COUNT'}, {'fieldName': 'ACREAGE', 'statistic': 'MEAN'}]