Create an Excel formula in python to check duplicate rows
Mar 10, 2022 Janaki Mahapatra, Python
import sys
import os
excel_columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
columns = []
for i in excel_columns:
columns.append( "$"+i+"$2:$"+i+"2,$"+i+"2")
excel_formula = ",".join(columns)
excel_formula = "=IF(COUNTIFS("+excel_formula+") > 1 , \"Duplicate row\", \"\")"
print(excel_formula)
## Output:
=IF(COUNTIFS($A$2:$A2,$A2,$B$2:$B2,$B2,$C$2:$C2,$C2,$D$2:$D2,$D2,$E$2:$E2,$E2,$F$2:$F2,$F2,$G$2:$G2,$G2,$H$2:$H2,$H2,$I$2:$I2,$I2,$J$2:$J2,$J2,$K$2:$K2,$K2,$L$2:$L2,$L2,$M$2:$M2,$M2,$N$2:$N2,$N2,$O$2:$O2,$O2,$P$2:$P2,$P2,$Q$2:$Q2,$Q2,$R$2:$R2,$R2,$S$2:$S2,$S2,$T$2:$T2,$T2,$U$2:$U2,$U2,$V$2:$V2,$V2,$W$2:$W2,$W2,$X$2:$X2,$X2,$Y$2:$Y2,$Y2,$Z$2:$Z2,$Z2) > 1 , "Duplicate row", "")