python文本转语音

发布时间 2023-03-25 20:08:22作者: 西北逍遥

 

pip install gtts

 

(wind_2021) J:\test>pip install gtts
Collecting gtts
  Downloading gTTS-2.3.1-py3-none-any.whl (28 kB)
Collecting requests<3,>=2.27
  Using cached requests-2.28.2-py3-none-any.whl (62 kB)
Requirement already satisfied: click<8.2,>=7.1 in e:\anaconda3\install\envs\wind_2021\lib\site-packages (from gtts) (8.1.3)
Requirement already satisfied: colorama in e:\anaconda3\install\envs\wind_2021\lib\site-packages (from click<8.2,>=7.1->gtts) (0.4.4)
Requirement already satisfied: certifi>=2017.4.17 in e:\anaconda3\install\envs\wind_2021\lib\site-packages (from requests<3,>=2.27->gtts) (2020.12.5)
Collecting charset-normalizer<4,>=2
  Downloading charset_normalizer-3.1.0-cp38-cp38-win_amd64.whl (96 kB)
     |████████████████████████████████| 96 kB 625 kB/s
Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\anaconda3\install\envs\wind_2021\lib\site-packages (from requests<3,>=2.27->gtts) (1.26.4)
Requirement already satisfied: idna<4,>=2.5 in e:\anaconda3\install\envs\wind_2021\lib\site-packages (from requests<3,>=2.27->gtts) (2.10)
Installing collected packages: charset-normalizer, requests, gtts
  Attempting uninstall: requests
    Found existing installation: requests 2.25.1
    Uninstalling requests-2.25.1:
      Successfully uninstalled requests-2.25.1
Successfully installed charset-normalizer-3.1.0 gtts-2.3.1 requests-2.28.2

(wind_2021) J:\test>
(wind_2021) J:\test>

 

from gtts import gTTS
import os

# Define the text to convert to speech
text = "Hello, world!"

# Create a gTTS object
tts = gTTS(text=text, lang="en")

# Save the speech as an MP3 file
tts.save("hello.mp3")

# Play the speech using the default audio player
os.system("mpg321 hello.mp3")

 

 

#################