Making speech-to-text in python is easier than other languages, just call pyttsx3 (module). But the voice recognition function of WIN11 system must be installed first
Specific viewing method: open the control panel, search for "speech recognition", if the following icon appears
![](https://img-blog.csdnimg.cn/img_convert/aec80854abdb46c0808f90c1aab857dd.png)
It means that the computer has a speech recognition module installed, otherwise it will be useless to toss. In particular, it is necessary to test whether the "text-to-speech conversion" function is useful, and it is useless if it does not work.
After the voice recognition module is installed on the computer
Then install the pypiwin32 module
Finally install the pyttsx3 module
import pyttsx3
engine = pyttsx3.init() # create object
engine.say('Hello, world!') # read the text
engine.say('Hello, world!') # read the text
engine.runAndWait() # Execute and wait for the voice to end
import pyttsx3
# create object
engine = pyttsx3.init()
# Get the current voice rate
rate = engine.getProperty('rate')
print(f'voice rate: {rate}')
# Set new speech rate
engine.setProperty('rate', 150)
# Get the current voice volume
volume = engine.getProperty('volume')
print(f'Voice volume: {volume}')
# Set the new voice volume, the minimum volume is 0 and the maximum volume is 1
engine.setProperty('volume', 1.0)
# Get the detailed information of the current voice and sound# I also found the example code here, which feels very contradictory, and the final voice is still a female voice
voices = engine.getProperty('voices')
print(f'Voice voice details: {voices}')
# Set the current voice as female, the current voice cannot read Chinese
engine.setProperty('voice', voices[1].id)
# Set the current voice voice as male, and the current voice can read Chinese
engine.setProperty('voice', voices[0].id)
# Get the current voice sound
voice = engine.getProperty('voice')
print(f'voice sound: {voice}')
# voice text
words = input('Please enter the words to say:')
# Speak the audio text
a = engine.say(words)
engine.runAndWait()
engine.stop()
# save the audio
engine.save_to_file("text.mp3")
engine.runAndWait()