Python Text to Speech Converter — Your Digital Narrator

Himani Bansal
Wiki Flood
Published in
6 min readApr 23, 2024

The Text to Speech (TTS) project with a graphical user interface (GUI) in Python is a versatile and user-friendly application designed to convert written text into spoken words. Utilizing the `pyttsx3` library for text-to-speech functionality and the `tkinter` library for GUI development, this project offers a seamless user experience.

The GUI features a spacious layout with a sky blue color scheme, creating a soothing and visually appealing interface. A prominent pink “Convert” button allows users to easily initiate the conversion process.

This Python Text-to-speech Converter project not only demonstrates the integration of text-to-speech technology into a GUI application but also serves as a practical tool for various applications, including accessibility tools, educational aids, and entertainment applications. With its customizable features and simple design, this TTS project provides a solid foundation for further development and customization to suit diverse user needs for easy interaction. Users can simply enter the text they wish to convert and press the button to hear it spoken aloud.

The Python Text-to-speech Converter project demonstrates the integration of text-to-speech functionality into a GUI application.

convert-text-to-speech-python
convert-text-to-speech-python

About Python Text to Speech Converter

The Text to Speech (TTS) project with a graphical user interface (GUI) in Python is a user-friendly application that converts text into spoken words.

The GUI allows users to input text, which is then converted into speech using the `pyttsx3` library. The interface features a sky blue color scheme for a pleasing aesthetic, with a prominent pink “Convert” button for easy interaction. Users can simply enter the text they wish to convert and press the button to hear it spoken aloud.

This python project serves as a practical example for anyone interested in developing similar applications or exploring TTS technology, showcasing the integration of text-to-speech functionality into a GUI application.

Prerequisites For Python Text to Speech Converter

  • pyttsx3: This library is used for text-to-speech functionality. You can install it using pip:
“ pip install pyttsx3 ”
  • tkinter: Tkinter is Python’s de-facto standard GUI (Graphical User Interface) package. It is included with most Python installations, so you typically don’t need to install it separately.

Python Text to Speech Converter Project File Structure

1. Main Window Creation: The project creates the main GUI window using the `tkinter` library. This window serves as the interface for the user to input text and interact with the application.

2. Text Entry Widget: A text entry widget (`Text` widget) is created within the main window. This widget allows users to enter the text that they want to convert to speech.

3. Convert Button: A “Convert” button is added to the GUI using the `ttk.Button` widget. This button triggers the conversion of the entered text to speech when clicked.

4. Text-to-Speech Conversion: When the “Convert” button is clicked, the text entered by the user is retrieved from the text entry widget. The `pyttsx3` library is then used to convert this text into speech.

5. Custom Styling: Optional, but in this project, a custom style is applied to the “Convert” button using the `ttk.Style` class. This custom style sets the foreground (text color) and background color of the button to pink.

Python Text to Speech Converter Project Implementation

1. Import Required Libraries:

Import the necessary libraries for the project.

import tkinter as tk
from tkinter import ttk
import pyttsx3

2. Define the Text-to-Speech Function:

Create a function to convert the text entered by the user into speech.

def convert_text_to_speech():
text = text_entry.get("1.0", "end-1c")
engine.say(text)
engine.runAndWait()
  • `text_entry.get(“1.0”, “end-1c”)`: This method retrieves the text entered by the user in the text entry widget. The arguments `”1.0"` and `”end-1c”` specify the range of text to retrieve, which in this case is from the first character (`”1.0"`) to the end of the text (`”end-1c”`).’
  • `engine.say(text)`: This method of the pyttsx3 engine (`engine`) is used to set the text that will be spoken.
  • `engine.runAndWait()`: This method is called to run the pyttsx3 engine and wait for the text to be spoken before continuing.

3. Create the Main Window:

Create the main window for the GUI application and set its title and background color.

# Create the main window
root = tk.Tk()
root.title("Flood-Text to Speech")
root.configure(bg='sky blue')
  • Tk()- class is created, representing the main window
  • ]title()- sets the window title
  • configure()- is used to set the background color

4. Create a Text Entry Widget:

Add a text entry widget to allow users to input text.

# Create a text entry widget
text_entry = tk.Text(root, height=20, width=60, bg='white')
text_entry.pack(padx=20, pady=20)

`tk.Text(root, height=20, width=60, bg=’white’)`: This creates a `Text` widget inside the `root` window with a height of 20 lines, a width of 60 characters per line, and a background color of white. The `Text` widget allows users to enter and edit multiple lines of text.

`text_entry.pack(padx=20, pady=20)`: This packs the `text_entry` widget into the `root` window with padding of 20 pixels on the x-axis (`padx`) and 20 pixels on the y-axis (`pady`). Packing a widget in Tkinter means adding it to the window so that it is visible and occupies the specified space.

5. Create a Convert Button:

Add a “Convert” button to trigger the text-to-speech conversion.

# Create a button to convert text to speech
convert_button = ttk.Button(root, text="Convert", command=convert_text_to_speech, style='Convert.TButton')
convert_button.pack(pady=10)

`ttk.Button`: This method creates a button widget. It takes several arguments, including the parent window (`root` in this case), the text to display on the button (`text=”Convert”`), the command to execute when the button is clicked (`command=convert_text_to_speech`), and the style of the button (`style=’Convert.TButton’`).

`pack(pady=10)`: The `pack` method is used to organize the button within the GUI window. The `pady=10` argument adds padding (empty space) of 10 pixels below the button, pushing it down from the previous widget (the text entry widget in this case). This helps to improve the spacing and layout of the GUI components.

6. Initialize the TTS Engine:

Initialize the pyttsx3 engine for text-to-speech conversion.

# Initialize the TTS engine
engine = pyttsx3.init()
  • `pyttsx3.init()` initializes the TTS engine. It creates an instance of the pyttsx3 engine, which is used to convert text into speech.

7. Configure Button Style:

Customize the style of the “Convert” button.

# Configure the button style
style = ttk.Style()
style.configure('Convert.TButton', foreground='Red', background='Red', font=('Arial', 12))

`ttk.Style()`: This method creates an instance of the `Style` class from the `ttk` module, which is used to configure the appearance of widgets in a tkinter GUI application. It allows you to define custom styles for different types of widgets, such as buttons, labels, and frames.

`style.configure(‘Convert.TButton’, foreground=’Red’, background=’Red’, font=(‘Arial’, 12))`: This method is used to configure the style of a specific type of widget, in this case, a button with the style name `’Convert.TButton’`. The `foreground` and `background` options set the text color and background color of the button, respectively. The `font` option sets the font family and size for the button text.

8. Run the Application:

Start the main event loop to run the GUI application.

# Run the main loop
root.mainloop()

Python Text to Speech Converter Code

import tkinter as tk
from tkinter import ttk
import pyttsx3


def convert_text_to_speech():
text = text_entry.get("1.0", "end-1c")
engine.say(text)
engine.runAndWait()


# Create the main window
root = tk.Tk()
root.title("Flood-Text to Speech")
root.configure(bg='sky blue')


# Create a text entry widget
text_entry = tk.Text(root, height=20, width=60, bg='white')
text_entry.pack(padx=20, pady=20)


# Create a button to convert text to speech
convert_button = ttk.Button(root, text="Convert", command=convert_text_to_speech, style='Convert.TButton')
convert_button.pack(pady=10)


# Initialize the TTS engine
engine = pyttsx3.init()


# Configure the button style
style = ttk.Style()
style.configure('Convert.TButton', foreground='Red', background='Red', font=('Arial', 12))


# Run the main loop
root.mainloop()

Python Text to Speech Converter Output

Python Text to Speech Converter Output
Python Text to Speech Converter Output
Python Text to Speech Converter
Python Text to Speech Converter

Conclusion

In conclusion, the Text to Speech (TTS) project with a graphical user interface (GUI) in Python provides a simple yet effective tool for converting text to speech with ease.

The project showcases the integration of the `pyttsx3` library for text-to-speech conversion and the `tkinter` library for creating a user-friendly GUI. The use of a sky blue color scheme and a pink “Convert” button adds a visually appealing touch to the interface.

This Python Text to Speech Converter Project serves as a practical example of how to create a basic TTS application with a GUI, demonstrating the potential for further customization and enhancement to meet specific user needs.

--

--

Himani Bansal
Wiki Flood

Doing my Best to Explain Data Science (Data Scientist, technology freak & Blogger)