ERP KPI Snapshot to Excel Python Script Generator
Many controllers rebuild the same KPI spreadsheet each month from an ERP export. This generator writes a short Python script that reads your CSV, calculates on time delivery by plant and saves a dated Excel file, so the monthly job runs in seconds.
Fill in
Python script
# Requires: pip install pandas openpyxl
import pandas as pd
from datetime import date
SOURCE = 'order_lines.csv'
GRACE_DAYS = 0
df = pd.read_csv(SOURCE, parse_dates=['promise_date', 'ship_date'])
# Only lines that have shipped count towards on time delivery
shipped = df[df['ship_date'].notna()].copy()
shipped['late_days'] = (shipped['ship_date'] - shipped['promise_date']).dt.days
shipped['on_time'] = shipped['late_days'] <= GRACE_DAYS
# Recompute from totals per plant, do not average percentages
by_plant = shipped.groupby('plant').agg(
lines=('on_time', 'size'),
on_time_lines=('on_time', 'sum'),
shipped_value=('line_value', 'sum'),
)
by_plant['otd_pct'] = (by_plant['on_time_lines'] / by_plant['lines'] * 100).round(1)
total_lines = int(by_plant['lines'].sum())
total_on_time = int(by_plant['on_time_lines'].sum())
company_otd = round(total_on_time / total_lines * 100, 1) if total_lines else 0.0
summary = pd.DataFrame({
'KPI': ['On time delivery percent (lines)', 'Shipped lines', 'Shipped value'],
'Value': [company_otd, total_lines, float(by_plant['shipped_value'].sum())],
})
out_name = 'kpi_snapshot_' + date.today().isoformat() + '.xlsx'
with pd.ExcelWriter(out_name, engine='openpyxl') as writer:
summary.to_excel(writer, sheet_name='Summary', index=False)
by_plant.reset_index().to_excel(writer, sheet_name='By plant', index=False)
shipped.sort_values('late_days', ascending=False).head(50).to_excel(writer, sheet_name='Worst 50 late lines', index=False)
print('Wrote', out_name)Export order lines from your ERP as CSV with a header row and ISO style dates, then check that date columns parse correctly. Treat the result as a starting point, test it against a manually counted sample, and keep exports on a secure location since they may contain customer data.
Retire the monthly spreadsheet rebuild
Optional: see how KPIs can refresh from ERP data without manual exports.
Optional. The tool and every export work without this. No spam.
How to use the generator
Export shipped and open order lines from your ERP into CSV, then enter the file name and the names of the promise date, ship date, value and plant columns. Copy the script, install pandas and openpyxl, and run it in the same folder as the CSV. It writes a workbook with a summary, a plant table and the 50 latest lines for follow up.
- Install pandas and openpyxl with pip
- Keep column names exactly as in the CSV header
- Set grace days to match your contract terms
The calculation
A shipped line is on time when the ship date minus the promise date is no more than the grace days. The script counts lines per plant and recomputes the percentage from totals. The company figure is built from summed lines, so large and small plants are weighted correctly instead of averaging plant percentages.
Extending it
Add more KPIs the same way: read another export, group, compute from totals and write a sheet. Schedule it with Windows Task Scheduler or cron after the nightly export. If you need quantity based or customer date based on time delivery, change the date columns and the filter rather than the structure.
Common mistakes
Including unshipped lines in the denominator, comparing text dates instead of parsed dates, and mixing partial shipments are the usual errors. Decide whether a partially shipped line counts on the first shipment or the last, and make the export match that rule.
Frequently Asked Questions
Do I need to know Python?
Not much. Install Python, install the two libraries, save the script as a file and run it. Changing column names and file names is done in the generator before copying.
Does it connect to my ERP?
No. It reads a CSV export, so it needs no database access or credentials. If you later connect to a database, keep it to a read-only account.
Why recompute from totals?
Averaging plant percentages gives a small plant the same weight as a large one. Summing on time lines and total lines first gives the correct company figure.
Can I use it for other KPIs?
Yes. Replace the on time logic with another formula, such as scrap rate from good and scrap quantities. The group, total and write structure stays the same.
If scripts like this multiply, Netray can provide a governed, read-only reporting layer on your ERP data so KPIs are not rebuilt by hand each month.
Related Tools
Inventory Aging Buckets SQL Generator
Build an inventory aging query with your own table and column names and bucket sizes, ready to paste into SQL Server style SQL.
Costing & FinanceManufacturing KPI Formula and Definitions Reference
A searchable lookup of the KPI formulas controllers and operations managers argue about, with units, typical ERP source and review cadence.
ERP OperationsERP Report Specification Template
A clean specification document for requesting a report from IT or a consultant, covering purpose, filters, calculations and acceptance tests.
ERP OperationsERP KPI Governance Maturity Assessment
Score how consistent, documented and trusted your KPIs are, and get next steps matched to your maturity band.
More Developer Tools tools
See all- 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
- ABAP Unit Test Class Generator