def fill_amounts_grouped_by_account(sh, sr, sc, transactions): data = {} for k, g in groupby(transactions, lambda t: t.account): group_sum(data, k, sum(r.amount for r in g)) for i, account in enumerate(sorted(data.keys())): sh[sr + i : sc].value = account sh[sr + i : sc + 2].value = data[account]
def fill_transactions(sh, sr, pivot, days): last_column = 1 + len(days) for d, c in days.items(): sh[sr:2 + c].value = str(d) sh.range(sr, sr, 2, last_column).style.align.horz.center() sh[sr:0].set_borders() sh[sr:1].set_borders() sh.range(sr, sr, 2, last_column).set_borders() sh[sr:last_column+1].value = 'Итого' sh[sr:last_column+1].set_borders() days_totals = {} i = sr + 1 for acc in sorted(pivot.keys()): sh[i:0].value = acc start = i acc_total = 0 for who in sorted(pivot[acc].keys()): sh[i:1].value = who who_total = 0 for day, amount in pivot[acc][who].items(): acc_total += amount who_total += amount group_sum(days_totals, day, amount) sh[i:2 + days[day]].value = amount sh[i:last_column+1].value = who_total i += 1 if len(pivot[acc]) > 1: sh[i-1:last_column+2].value = acc_total sh.range(start, i - 1, 0, 0).set_borders(0) sh.range(start, i - 1, 1, 1).set_borders() sh.range(start, i - 1, 2, last_column).set_borders() sh.range(start, i - 1, last_column + 1, last_column + 1).set_borders() # fill total_row sh[i:0].value = 'Итого' sh[i:last_column + 1].value = sum(days_totals.values()) sh.range(i,i,0,1).set_borders(0) sh.range(i, i, 2, last_column).set_borders() sh[i:last_column+1].set_borders() for day, amount in days_totals.items(): sh[i:2 + days[day]].value = amount