We previously showed that the Chinese herbal medicine, Shaofu Zhuyu decoction (SFZYD), shrank the size of endometriotic lesions in rats with endometriosis. We therefore conducted the present study to investigate the effects of letrozole and SFZYD on gut microbiota in endometriotic rats. Rats were divided into four groups: a blank group, model group, letrozole group, and SFZY group. Ectopic lesion size and COX-2 expression in the endometrium and endometriotic lesions were compared, and the community of gut microbiota was detected using 16S rRNA gene sequencing. Both letrozole and SFZYD reduced the size of ectopic lesions as well as lowered the expression of COX-2, thus reducing the inflammatory response. Compared with the blank group, the α-diversity of gut microbiota in endometriotic rats decreased, the Firmicutes/Bacteroidetes ratio increased, and the abundance of Ruminococcaceae was reduced. The α-diversity of gut microbiota in the letrozole group was similar to that in the model group, but the Firmicutes/Bacteroidetes ratio was diminished. The α-diversity in the SFZY group was similar to that in the blank group, the Firmicutes/Bacteroidetes ratio was attenuated, and the abundance of Ruminococcaceae was elevated compared with the model group. These results indicated that the therapeutic mechanisms of both letrozole and SFZYD were related to the restoration of gut microbiota.