#+begin_src python import random from collections import namedtuple Tier = namedtuple('Tier', ['number', 'total']) def generate_table(tiers): total = sum(tier.total for tier in tiers) total_numbers = sum(tier.number for tier in tiers) # A None in a list will result in a hline in org tables return [ None, # Header separator ('Tier', 'Number', 'Total', '% of Total'), None, # Column header separator ,*[ (i+1, tier.number, f'{tier.total:,.2f}', f'{100 * tier.total / total:.2f}') for i, tier in enumerate(tiers) ], None, # Footer separator ('Total', total_numbers, f'{total:,.2f}', '100.00'), None, # Final separator ] tiers = [ Tier(4, random.uniform(1000, 10000)), Tier(19, random.uniform(1000, 10000)), Tier(24, random.uniform(1000, 10000)), ] return generate_table(tiers) #+end_src #+RESULTS: |-------+--------+-----------+------------| | Tier | Number | Total | % of Total | |-------+--------+-----------+------------| | 1 | 4 | 8,022.65 | 65.45 | | 2 | 19 | 1,549.71 | 12.64 | | 3 | 24 | 2,685.28 | 21.91 | |-------+--------+-----------+------------| | Total | 47 | 12,257.65 | 100.00 | |-------+--------+-----------+------------| #+ATTR_LATEX: :align rrrr :float t :placement [htbp] #+RESULTS: