I am trying to go into some advanced(advanced for me at least) document editing automation I wrote something working but I feel I wrote the code in a very crude way. I would like to find a more elegant way to do this if possible and any help is appreciated. This is my original working code below:
j = 0
for i in range(80,121):
df[f"Bonus Payout {i}%"]=df["Monthly gross salary 100% (LC)"]*j
df[f"Bonus Payout {i}%"]=df[f'Bonus Payout {i}%'].apply('{:,.2f}'.format)
j += 0.01
for i in range(80, 121):
globals()[f"bonus_{i}"] = df[f"Bonus Payout {i}%"].values
for i in range(80,100):
df[f"Monthly gross salary {i}% - Base (LC)"]=df[f'Monthly gross salary {i}% - Base (LC)'].apply('{:,.2f}'.format)
for i in range(101,120):
df[f"Monthly gross salary {i}% - Base (LC)"]=df[f'Monthly gross salary {i}% - Base (LC)'].apply('{:,.2f}'.format)
df["Monthly gross salary 100% (LC)"]=df["Monthly gross salary 100% (LC)"].apply('{:,.2f}'.format)
df["Monthly gross salary 120% (LC)"]=df["Monthly gross salary 120% (LC)"].apply('{:,.2f}'.format)
df.replace({"Name (First Name/Last Name)": {'/': '-'}}, regex=True, inplace=True) ##to correct filenames
# Formatting the salaries and regular expression
Name=df["Name (First Name/Last Name)"].values
for i in range(80, 100):
globals()[f"month_{i}"] = df[f"Monthly gross salary {i}% - Base (LC)"].values
month_100=df["Monthly gross salary 100% (LC)"].values
for i in range(101, 120):
globals()[f"month_{i}"] = df[f"Monthly gross salary {i}% - Base (LC)"].values
month_120=df["Monthly gross salary 120% (LC)"].values
localc=df["Local Currency"].values
Until so far I am happy with my code, but the my issue is with the code below, as you see it is very ugly
zipped = zip(Name,month_80,month_81,month_82,month_83,month_84,month_85,month_86,month_87,month_88,month_89,month_90,
month_91,month_92,month_93,month_94,month_95,month_96,month_97,month_98,month_99,
month_100,month_101,month_102,month_103,month_104,month_105,month_106,month_107,month_108,month_109,month_110,
month_111,month_112,month_113,month_114,month_115,month_116,month_117,month_118,month_119,month_120,localc,bonus_80,bonus_81,bonus_82,bonus_83,bonus_84,bonus_85,bonus_86,bonus_87,bonus_88,bonus_89,bonus_90,
bonus_91,bonus_92,bonus_93,bonus_94,bonus_95,bonus_96,bonus_97,bonus_98,bonus_99,
bonus_100,bonus_101,bonus_102,bonus_103,bonus_104,bonus_105,bonus_106,bonus_107,bonus_108,bonus_109,bonus_110,
bonus_111,bonus_112,bonus_113,bonus_114,bonus_115,bonus_116,bonus_117,bonus_118,bonus_119,bonus_120)
for 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,ca,cb,cc,cd,ce,cf,cg,ch,ci,cj,ck,cl,cm,cn,co,cp,cq,bx,cx,dx,ex,fx,gx,hx,ix,jx,kx,lx,mx,nx,ox,px,qx,rx,sx,tx,ux,vx,wx,xx,yx,zx,cax,cbx,ccx,cdx,cex,cfx,cgx,chx,cix,cjx,ckx,clx,cmx,cnx,cox,cpx in zipped:
doc=DocxTemplate(file)
context = {"Name":a,"month_80":b,"month_81":c,"month_82":d,"month_83":e,"month_84":f,"month_85":g,"month_86":h,
"month_87":i,"month_88":j,"month_89":k,"month_90":l,
"month_91":m,"month_92":n,"month_93":o,"month_94":p,"month_95":q,"month_96":r,"month_97":s,"month_98":t,
"month_99":u,"month_100":v,
"month_101":w,"month_102":x,"month_103":y,"month_104":z,"month_105":ca,"month_106":cb,"month_107":cc,
"month_108":cd,"month_109":ce,"month_110":cf,
"month_111":cg,"month_112":ch,"month_113":ci,"month_114":cj,"month_115":ck,"month_116":cl,"month_117":cm,
"month_118":cn,"month_119":co,"month_120":cp,"localc":cq,"bonus_80":bx,"bonus_81":cx,"bonus_82":dx,
"bonus_83":ex,"bonus_84":fx,"bonus_85":gx,"bonus_86":hx,"bonus_87":ix,"bonus_88":jx,"bonus_89":kx,
"bonus_90":lx,"bonus_91":mx,"bonus_92":nx,"bonus_93":ox,"bonus_94":px,"bonus_95":qx,"bonus_96":rx,
"bonus_97":sx,"bonus_98":tx,"bonus_99":ux,"bonus_100":vx,"bonus_101":wx,"bonus_102":xx,"bonus_103":yx,
"bonus_104":zx,"bonus_105":cax,"bonus_106":cbx,"bonus_107":ccx,"bonus_108":cdx,"bonus_109":cex,
"bonus_110":cfx,"bonus_111":cgx,"bonus_112":chx,"bonus_113":cix,"bonus_114":cjx,"bonus_115":ckx,
"bonus_116":clx,"bonus_117":cmx,"bonus_118":cnx,"bonus_119":cox,"bonus_120":cpx}
doc.render(context)
doc.save(date+" "+'{}.docx'.format(a))
print("All Documents are created successfully")