Gram staining can classify bacterial species into two large groups based on cell wall differences. Our study revealed that within the same Gram group (Gram-positive or Gram-negative), subtle cell wall variations can alter staining outcomes, with the peptidoglycan layer and lipid content significantly influencing this effect. Thus, bacteria within the same group can also be differentiated by their spectra. Using hyperspectral microscopy, we identified six species of intestinal bacteria with 98.1% accuracy. Our study also demonstrated that selecting the right spectral band and background calibration can enhance the model’s robustness and facilitate precise identification of varying sample batches. This method is suitable for analyzing bacterial community pathologies.