Master-detail reports display hierarchical data where each master record (parent) has associated detail records (children). GroupDocs.Assembly for Python via .NET supports master-detail reports through nested foreach data bands, allowing you to display parent records with their related child records.
Here are the steps to generate master-detail reports:
Define data structures with parent-child relationships
Create a template with nested foreach data bands
Outer foreach iterates over master records
Inner foreach iterates over detail records for each master
Assemble the document with your hierarchical data
Note
Nested foreach data bands allow you to create complex hierarchical reports. The outer band processes master records, while inner bands process related detail records.
Define Master-Detail Data Structure
In Python, no classes are needed: a master record is a dict, and one of its keys holds the list of its detail records, each a dict too. Every order below has an Items list:
The outer band spans both rows: it opens in the first cell of the order row and closes in the last cell of the item row. The inner band is the item row alone, so the table gets one order row followed by one row per item.
Using Aggregates in Master-Detail
Calculate totals and aggregates for each master record:
fromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerorders=[{"OrderId":1001,"CustomerName":"Quayside Bistro","Items":[{"ProductName":"Harbor Blend whole bean coffee","Quantity":2,"Price":Decimal("18.40")},{"ProductName":"Oat and honey biscuits","Quantity":10,"Price":Decimal("3.20")},],},]assembler=DocumentAssembler()assembler.assemble_document("order-items-subtotals.docx","order-items-subtotals-report.docx",DataSourceInfo(orders,"orders"))print("Master-detail report with aggregates generated successfully.")
When using nested foreach data bands, ensure proper closing tags. Each opening <<foreach>> must have a corresponding closing <</foreach>>, and an inner band must close before the band around it.
Advanced Usage Topics
To learn more about complex hierarchical structures, multiple levels of nesting, and advanced master-detail patterns, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: