For this:
1) You need to pay for fundamental data
2) Make the data compatible with excel
3) Classify stocks into sectors
4) Calculated a weighted average PE of the stocks which represent the sector.
5) Do PE comparision
The process would have been simplified had you been living in the US (Plenty of data vendors) or Australia (Paritech provides metamarket +).
If you want to do this for Indian stocks, it's going to cost you around 15K a year and hell of a lot of time coding and recoding
Maybe someone has a better solution