Zamiana tekstu na mowę w python – pyttsx3

Istnieje wiele modułów, które potrafią zamienić tekst na mowę. Jednym z nich jest biblioteka pyttsx3, który można zainstalować za pomocą pip:

pip install pyttsx3

na windowsie, podczas instalacji pyttsx3 zostaną zainstalowane też dodatkowe moduły (np. pypiwin32)

Dokumentacja i przykłady korzystania z biblioteki:

pypi – https://pypi.org/project/pyttsx3/

pyttsx3 docs – https://pyttsx3.readthedocs.io/en/latest/

Prosty przykład programu zamieniającego tekst na mowę:

import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()

Sprawdzenie dostępnych głosów i ustawienie głosu polskiego Paulina

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
    print("Voice ", voice.name)
    print("Voice ID ", voice.id)


engine.setProperty('voice', "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_PL-PL_PAULINA_11.0") 
engine.say("Cześć ludziska")
engine.runAndWait()

Przykładowy program zamiany tekstu na mowę za pomocą modułu gtts

# Import the required module for text 
# to speech conversion
from gtts import gTTS
  
# This module is imported so that we can 
# play the converted audio
import os
  
# The text that you want to convert to audio
mytext = 'Cześć. Jak się masz?'
  
# Language in which you want to convert
language = 'pl'
  
# Passing the text and language to the engine, 
# here we have marked slow=False. Which tells 
# the module that the converted audio should 
# have a high speed
myobj = gTTS(text=mytext, lang=language, slow=False)
  
# Saving the converted audio in a mp3 file named
# welcome 
myobj.save("welcome.mp3")
  
# Playing the converted file
os.system("welcome.mp3")

Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Scroll to Top