Spermidine, which can be synthesized by the gut microbiota, can prevent cardiac hypertrophy and delay the progression to heart failure (HF). However, it is not clear whether the effect of spermidine on cardiac function is mediated by modulating the gut microbiota when HF occurs. Female HF Kunming mice induced by transverse aortic constriction were administered spermidine (HF+S group) or its antagonist (HF+SR group). Echocardiography, messenger ribonucleic acid (RNA) and protein expression of galectin-3 in the heart, cardiomyocyte apoptosis assays and gut microbiota analysis were detected. Left ventricular end-diastolic volume and diameter (LVVd and LVDd), and left ventricular end-systolic volume and diameter in the HF+SR group were significantly enlarged compared with those in the HF group (all P < 0.05). The HF+S group had a smaller LVDd and LVVd than the HF+SR group (5.01 ± 0.67 vs. 6.13 ± 0.45 mm, P = 0.033; 121.44 ± 38.74 vs. 189.94 ± 31.42 μL, P = 0.033). The messenger RNA and protein expression of galectin-3 and the number of apoptotic cardiomyocytes increased significantly in the HF+SR group compared to the HF group. Gut microbiota analysis showed that spermidine antagonists reduced the Firmicutes/Bacteroidetes ratio and changed the microbial community richness and diversity. In conclusion, spermidine can improve cardiac function in HF, and the regulation of gut microbiota and cardiac fibrosis may be a factor in the effect of spermidine on the improvement of cardiac function.