GSTR-1 HSN Summary Python (pandas) Script Generator
Preparing the HSN-wise summary for GSTR-1 by hand from thousands of invoice lines is slow and error prone. A short pandas script groups the lines for you and checks the totals tie out.
Fill in
HSN summary script
import pandas as pd
df = pd.read_csv('invoice_lines.csv', dtype={'hsn_code': str})
# Clean HSN: strip spaces and keep digits only
df['hsn_code'] = df['hsn_code'].astype(str).str.replace(r'\D', '', regex=True)
num_cols = ['qty', 'taxable_value', 'igst_amount', 'cgst_amount', 'sgst_amount']
for c in num_cols:
df[c] = pd.to_numeric(df[c], errors='coerce').fillna(0)
summary = (
df.groupby(['hsn_code', 'uom', 'gst_rate'], as_index=False)
.agg({
'item_description': 'first',
'qty': 'sum',
'taxable_value': 'sum',
'igst_amount': 'sum',
'cgst_amount': 'sum',
'sgst_amount': 'sum',
})
)
summary['total_value'] = (
summary['taxable_value'] + summary['igst_amount']
+ summary['cgst_amount'] + summary['sgst_amount']
)
summary = summary.round(2)
summary.to_excel('hsn_summary.xlsx', index=False)
print('HSN rows:', len(summary))
print('Taxable total:', round(summary['taxable_value'].sum(), 2))Quick tie-out check
# Compare with the source file totals before using the summary
src_taxable = round(df['taxable_value'].sum(), 2)
sum_taxable = round(summary['taxable_value'].sum(), 2)
assert src_taxable == sum_taxable, 'Taxable value does not tie out'
blank_hsn = df[df['hsn_code'] == '']
print('Lines with blank HSN:', len(blank_hsn))Requires pandas and openpyxl. The GST portal's table layout, UQC list and digit rules change, so use the output as a working summary and check it against the current GSTR-1 instructions. Credit notes should be netted or handled separately, as your return preparation process requires. Test on a small file first.
GST data pipeline from ERP
Optional discussion on automating GST return data extraction from your ERP.
Optional. The tool and every export work without this. No spam.
How to use the script generator
Export invoice lines from your ERP to CSV with HSN, unit, rate, quantity, taxable value and the three tax amounts. Enter the file name and your column names. Copy the script into a Python file, install pandas and openpyxl, and run it. The output Excel file lists one row per HSN, unit and rate with quantity and values. The second block checks that the summary taxable value equals the source file and counts lines with a blank HSN.
- Export lines, not invoice headers
- Keep the HSN column as text so leading zeros survive
- Run the tie-out block before you trust the summary
The logic behind the summary
The portal expects values grouped by HSN, with description, unit of quantity, total quantity, total value, taxable value and the tax amounts. Grouping by HSN, unit and rate keeps lines with different rates from being merged, which is the usual source of rounding disputes. The script treats missing numbers as zero, strips non-digit characters from HSN and rounds the final result to two decimals.
Worked example
A plant exports 5,000 invoice lines for a month. After running the script, 180 HSN, unit and rate combinations remain. The tie-out check confirms the taxable total equals the ERP report. It also reports 12 lines with blank HSN, which you fix at the source and rerun. Without the check, those 12 lines would have disappeared into one blank group and produced a mismatch only after filing.
Common mistakes
Reading HSN as a number drops leading zeros, so always read it as text. Mixing credit notes into the same file without a sign convention gives wrong quantities. Using different unit spellings, such as NOS and Nos, creates duplicate groups, so standardise units first. Finally, remember the script prepares data and does not file anything for you; review the result with your GST adviser.
Frequently Asked Questions
Which libraries do I need?
You need Python with pandas and openpyxl installed. Pandas does the grouping and openpyxl writes the Excel file. Install them with pip and run the script from a folder that contains your CSV export.
Does the script work for credit notes?
Not on its own. Credit notes need to be netted off or shown separately as your return process requires. Add a sign column or filter by document type before grouping, and confirm treatment with your GST adviser.
Why group by rate as well as HSN?
The same HSN can be sold at different rates, for example because of conditions or a notification change mid-month. Grouping by rate keeps those lines apart so each summary row has one rate.
Can I upload the Excel file directly to the GST portal?
Not as is. The portal uses its own offline tool or JSON format with defined columns and unit codes. Use this output as a working summary and map it into the official upload format.
If you want return preparation data pulled directly from ERP tables with reconciliation reports, we can help build that on your own systems.
Related Tools
ERP Item Master HSN and GST Rate Audit SQL Generator
Enter your item master and invoice line table and column names to get SQL that finds blank HSN codes, wrong digit counts, items with several rates and invoice lines that disagree with the master.
Country & TaxIndia HSN Chapter Guide for Manufacturing Inputs
A searchable list of the HSN chapters most used by Indian manufacturers, with a plain description and typical use. Use it to narrow down the chapter before you look up the full 8-digit code.
Country & TaxE-Way Bill Bulk Upload JSON Generator (India)
Fill in the invoice, parties, vehicle and one item line to produce the JSON structure used for bulk generation of e-way bills.
Country & TaxIndia ERP GST Readiness Assessment for Manufacturers
Ten questions that test whether your ERP handles the main GST workflows for a manufacturer without manual workarounds, with a score and next steps.
More Developer Tools tools
See all- GSTIN Validator Code Generator
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator