The accumulation of heavy metals in the soil not only causes serious damage to the soil ecosystem, but also threatens human health through the food chain. Exopolysaccharides have the functions of adsorbing and chelating heavy metals and reducing their bioavailability in the soil. In our study, exopolysaccharide-producing bacteria with a high efficiency in adsorbing cadmium (Cd) and lead (Pb) were screened from heavy metal-contaminated farmland. Through pot experiments, the influence of functional strains on the size distribution, heavy metal content, and bacterial community structure of soil aggregates in lettuce was studied using high-throughput sequencing technology. The results show that 11 strains secreting exopolysaccharides were initially screened from heavy metal-contaminated soil. Among them, strain Z23 had a removal rate of 88.6% for Cd and 93.2% for Pb. The rate at which Cd was removed by strain Z39 was 92.3%, and the rate at which Pb was removed was 94.4%. Both strains belong to Bacillus sp. Strains Z23 and Z39 induced the formation of Fe2Pb(PO4)2, Cd2(PO4)2, and Pb2O3 in the solution. The pot experiments showed that strains Z23 and Z39 increased (19.1~23.9%) the dry weight and antioxidant enzyme activity of lettuce roots and leaves, while reducing (40.1~61.7%) the content of Cd and Pb. Strains Z23 and Z39 increased the proportion of microaggregates (<0.25 mm) and the content of exopolysaccharides in rhizosphere soil and reduced (38.4–59.7%) the contents of available Cd and Pb in microaggregates, thus inhibiting the absorption of heavy metals by lettuce. In addition, the exopolysaccharide content and the bacterial community associated with heavy metal resistance and nitrogen (N) cycling (Patescibacteria, Saccharimonadales, Microvirga, and Pseudomonas) in microaggregates were key factors affecting the available heavy metal content in soil. These results show that the exopolysaccharide-producing bacteria Z23 and Z39 reduced the absorption of Cd and Pb by lettuce tissues, thus providing strain resources for the safe utilization of soils that exceed heavy metal standards for farmland and for reducing the heavy metal content in vegetables.