from gtts import gTTS import os def text_to_speech(text, language='zh-cn'): # Create a gTTS object tts = gTTS(text=text, lang=language, slow=False) # Save the speech as an audio file tts.save("output.mp3") # Play the audio file os.system("mpg321 output.mp3") # Make sure you have mpg321 installed if __name__ == "__main__": text = "你好,这是在进行自动驾驶测试。" text_to_speech(text)