Text-to-Speech Converter in Python: Turn Text into Audio Files

Text-to-Speech Converter in Python: Turn Text into Audio Files

Would you wish to convert your textual content paperwork or articles into speech you can hearken to wherever you need? This Python script makes it simple for you. It takes textual content enter from a file and generates an audio output in mp3 or .wav format. Then you’ll be able to put your ft up and hearken to it whereas driving, figuring out, or simply chilling.

We will make the most of pyttsx3, an offline text-to-speech conversion library. You shouldn’t have to do something however simply point out the identify of your textual content file and the identify the audio file will probably be saved as. The script does the remainder!

It is a wonderful instrument for readers who hear once they learn, or if you wish to generate audio variations of your recordsdata. Try and see your content material in a recent method!

Text-to-Speech Converter Python Script

Here’s a Python script for a Text-to-Speech Converter that reads textual content from a file, converts it to speech, and saves it as an audio file utilizing the pyttsx3 library:


import pyttsx3
import os

def text_to_speech(file_path, output_audio_file):
    # Initialize the text-to-speech engine
    engine = pyttsx3.init()

    attempt:
        # Check if the file exists
        if not os.path.exists(file_path):
            print(f"File not discovered: {file_path}")
            return

        # Read the content material of the file
        with open(file_path, 'r', encoding='utf-8') as file:
            textual content = file.learn()

        if not textual content.strip():
            print("The file is empty.")
            return

        # Convert textual content to speech
        print("Converting textual content to speech...")
        engine.save_to_file(textual content, output_audio_file)

        # Wait for the conversion to finish
        engine.runAndWait()

        print(f"Audio file saved as: {output_audio_file}")
    besides Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    # Input textual content file path
    text_file_path = enter("Enter the trail to the textual content file: ").strip()

    # Output audio file path
    output_audio_file_path = enter("Enter the trail to avoid wasting the audio file (e.g., output.mp3): ").strip()

    # Convert textual content to speech
    text_to_speech(text_file_path, output_audio_file_path)

How to Use?

These steps will enable you to make use of this script:

  1. Fistly set up the Python pyttsx3 module, if not already put in. You can even think about to create a separate Python digital setting.
    pip set up pyttsx3
    
  2. Save the script as text_to_speech.py.
  3. Run the script:
    python text_to_speech.py
    
    Python Text-to-Speach
    Python script for textual content to audio file converter

    Provide the trail to a textual content file and specify the specified identify and site for the output audio file (e.g., output.mp3).

    You can customise the speech properties (e.g., velocity, quantity, voice) by configuring the pyttsx3 engine settings.

    The put up Text-to-Speech Converter in Python: Turn Text into Audio Files appeared first on TecAdmin.

HI-FI News

through TecAdmin https://tecadmin.internet

November 21, 2024 at 08:21AM