Remove duplicate Excel records with Python
Keep the first data row for each complete typed row and write a separate values-only workbook.
View code and setup
Before you start
- Install Python 3. Create the environment below, save the Python example as task.py, then run .venv/bin/python task.py in the folder containing input.xlsx. On Windows, use py -m venv .venv and .venv\Scripts\python.exe in place of .venv/bin/python. Replace Inventory with your worksheet name.
python3 -m venv .venv
.venv/bin/python -m pip install openpyxlfrom openpyxl import Workbook
from openpyxl import load_workbook
workbook = load_workbook("input.xlsx", read_only=True, data_only=True)
sheet = workbook["Inventory"]
rows = sheet.iter_rows(values_only=True)
output = Workbook()
target = output.active
target.title = "Inventory"
target.append(next(rows))
seen = set()
for row in rows:
key = tuple((type(value).__name__, value) for value in row)
if key not in seen:
seen.add(key)
target.append(row)
workbook.close()
output.save("deduplicated.xlsx").venv/bin/python task.pyLimits and output
- These data recipes use saved cell results, not recalculated formulas. They do not copy charts, styles, macros or workbook relationships. Keep the original file.
- The first row is retained as a header. This compares the entire row, with numeric and text values distinct; change the key explicitly for selected-column matching. It does not safely remove rows from a complex source workbook. Review formula-looking source text before writing it.
Documentation: openpyxl tutorial and workbook loading options