# GSTR-1 HSN Summary Python (pandas) Script Generator

> Source: https://netray.co/tools/gstr-1-hsn-summary-pandas-script-generator-india
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Name the columns in your ERP invoice line export and get a pandas script that rolls them up into an HSN-wise summary with quantity, value, taxable value and taxes.

## Category

developer

## Region

India

## Audience

- GST accountant
- ERP developer
- Finance analyst
- Python user

## Tool

**Kind:** template

**Output:** code

**Language:** python

**Fields:**

#### Input CSV file

**Key:** input_file

**Type:** text

**Default:** invoice_lines.csv

#### Output Excel file

**Key:** output_file

**Type:** text

**Default:** hsn_summary.xlsx

#### HSN column

**Key:** col_hsn

**Type:** text

**Default:** hsn_code

#### Description column

**Key:** col_desc

**Type:** text

**Default:** item_description

#### Unit (UQC) column

**Key:** col_uqc

**Type:** text

**Default:** uom

#### Tax rate column

**Key:** col_rate

**Type:** text

**Default:** gst_rate

#### Quantity column

**Key:** col_qty

**Type:** text

**Default:** qty

#### Taxable value column

**Key:** col_taxable

**Type:** text

**Default:** taxable_value

#### IGST column

**Key:** col_igst

**Type:** text

**Default:** igst_amount

#### CGST column

**Key:** col_cgst

**Type:** text

**Default:** cgst_amount

#### SGST column

**Key:** col_sgst

**Type:** text

**Default:** sgst_amount

**Blocks:**

#### HSN summary script

**Body:** import pandas as pd

df = pd.read_csv('{{input_file}}', dtype={'{{col_hsn}}': str})

# Clean HSN: strip spaces and keep digits only
df['{{col_hsn}}'] = df['{{col_hsn}}'].astype(str).str.replace(r'\D', '', regex=True)

num_cols = ['{{col_qty}}', '{{col_taxable}}', '{{col_igst}}', '{{col_cgst}}', '{{col_sgst}}']
for c in num_cols:
    df[c] = pd.to_numeric(df[c], errors='coerce').fillna(0)

summary = (
    df.groupby(['{{col_hsn}}', '{{col_uqc}}', '{{col_rate}}'], as_index=False)
      .agg({
          '{{col_desc}}': 'first',
          '{{col_qty}}': 'sum',
          '{{col_taxable}}': 'sum',
          '{{col_igst}}': 'sum',
          '{{col_cgst}}': 'sum',
          '{{col_sgst}}': 'sum',
      })
)

summary['total_value'] = (
    summary['{{col_taxable}}'] + summary['{{col_igst}}']
    + summary['{{col_cgst}}'] + summary['{{col_sgst}}']
)

summary = summary.round(2)
summary.to_excel('{{output_file}}', index=False)
print('HSN rows:', len(summary))
print('Taxable total:', round(summary['{{col_taxable}}'].sum(), 2))

#### Quick tie-out check

**Body:** # Compare with the source file totals before using the summary
src_taxable = round(df['{{col_taxable}}'].sum(), 2)
sum_taxable = round(summary['{{col_taxable}}'].sum(), 2)
assert src_taxable == sum_taxable, 'Taxable value does not tie out'
blank_hsn = df[df['{{col_hsn}}'] == '']
print('Lines with blank HSN:', len(blank_hsn))

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use the script generator

**Body:** 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.

**Bullets:**

- 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

**Body:** 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

**Body:** 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

**Body:** 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.

**Cta:** If you want return preparation data pulled directly from ERP tables with reconciliation reports, we can help build that on your own systems.

**Faq:**

#### Which libraries do I need?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

gstr-1 hsn summary python, hsn wise summary excel pandas, gstr1 table 12 hsn summary script, erp invoice export to hsn summary, pandas groupby hsn gst, gst hsn summary automation, python script gst return preparation, hsn summary csv generator

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
