This guide will walk you through the process of setting up a text-to-speech program using the Google Text-to-Speech (gTTS) library in Windows Subsystem for Linux (WSL2). We'll be using the Ubuntu distribution as an example, but you can choose any Linux distribution within WSL2.
- WSL2 installed and set up on your Windows machine.
- Basic familiarity with the Linux command line.
-
Install WSL2 if you haven't already. You can find instructions on the Microsoft website or various online tutorials.
-
Set up a Linux distribution within WSL2. For example, to install Ubuntu, open the Microsoft Store, search for Ubuntu, and follow the installation steps.
-
Open your WSL2 terminal.
-
Update the package lists by running the following command:
sudo apt update
-
Install the required packages:
sudo apt install python3 python3-pip mpg123
- Optional: Could be create a venv in your folder:
python3 -m venv venv # Next activate venv: source venv/bin/activate
-
In the WSL2 terminal, install the gTTS library using pip:
pip3 install gTTS
-
Open a text editor within WSL2, such as Nano or Vim.
-
Create a new Python script. For example, you can name it
tts.py
. -
Paste the following code into the script:
from gtts import gTTS import os def text_to_speech(text, filename): tts = gTTS(text) tts.save(filename) if __name__ == "__main__": text = "Hello, World!" # Replace with your desired text filename = "output.mp3" # Output file name text_to_speech(text, filename) os.system("mpg123 " + filename)
-
Modify the
text
variable with the desired text you want to convert to speech. -
Save the file and exit the text editor.
-
In the WSL2 terminal, navigate to the directory where you saved the
tts.py
file. -
Run the script using the following command:
python3 main.py
-
The program will generate an MP3 file with the speech output and play it using the
mpg123
command.
Congratulations! You have successfully set up a text-to-speech program using the gTTS library in WSL2. Feel free to modify the script and experiment with different texts and options to suit your specific needs.