How to group repeated products from a table in a custom printing format?

I am making a custom printing format, where I want to group the repeated items in a single line in a table and this adds the number of items and the total between them.

What I have for the moment:

<table class="table table-bordered table-condensed">
		<th width="40%">{{ _("Item") }}</th>
		<th width="30%" class="text-right">{{ _("Qty") }}</th>
		<th width="30%" class="text-right">{{ _("Amount") }}</th>
	      {%- for item in doc.items -%}
			{{ item.item_code }}
			{%- if item.item_name != item.item_code -%}
				<br>{{ item.item_name }}
			{%- endif -%}
			{%- if item.gst_hsn_code -%}
				<br><b>{{ _("HSN/SAC") }}:</b> {{ item.gst_hsn_code }}
			{%- endif -%}
			{%- if item.serial_no -%}
				<br><b>{{ _("Serial No") }}:</b> {{ item.serial_no }}
			{%- endif -%}
		<td class="text-right">{{ item.qty }}</td>
		<td class="text-right">{{ item.get_formatted("amount") }}</td>
	{%- endfor -%}

did you solve

See below :