gpt-4-0314· openai
Suzhen/CodeChat
write me a python program to convert the text in the file into voice speech
- User
write me a python program to convert the text in the file into voice speech
- Assistant
To create a Python program that converts text in a file into speech, you can use the "gTTS" (Google Text-to-Speech) library. First, make sure you have the library installed in your Python environment. You can install it using pip:
pip install gttsHere is a simple Python program that converts the text in a file into speech and saves the output as an MP3 file:
from gtts import gTTS import os def text_to_speech(input_file, output_file): with open(input_file, 'r') as file: text = file.read() tts = gTTS(text, lang='en') tts.save(output_file) if __name__ == '__main__': input_file = 'input_text.txt' output_file = 'output_speech.mp3' text_to_speech(input_file, output_file) print(f"Speech saved to '{output_file}'")Replace
input_text.txtwith the name of the text file you want to convert into speech. The output speech will be saved as an MP3 file (output_speech.mp3in this case).You can change the language by modifying the
langparameter in thegTTSfunction. For example, you can uselang='es'for Spanish orlang='fr'for French.