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">
<thead>
	<tr>
		<th width="40%">{{ _("Item") }}</th>
		<th width="30%" class="text-right">{{ _("Qty") }}</th>
		<th width="30%" class="text-right">{{ _("Amount") }}</th>
	</tr>
</thead>
    <tbody>
	      {%- for item in doc.items -%}
	    <tr>
		<td>
			{{ 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>
		<td class="text-right">{{ item.qty }}</td>
		<td class="text-right">{{ item.get_formatted("amount") }}</td>
	</tr>
	{%- endfor -%}
</tbody>
</table>

did you solve

See below :