Learn how to use **Google Text-to-Speech API** via Google Cloud Platform (GCP) with Python in this step-by-step guide. Discover how to convert text into lifelike speech for applications in voice assistants, audiobooks, and more. Follow along as we set up the API on GCP, write Python code, and synthesize text to speech effortlessly. — ### Steps to Use Google Text-to-Speech API via GCP with Python #### 1. **Set Up Google Text-to-Speech API on GCP** 1. **Create a New Project**: – Visit [Google Cloud Console](https://console.cloud.google.com/) and create a new project. 2. **Enable the API**: – Navigate to the **API & Services** section. – Search for **Cloud Text-to-Speech API** and enable it. 3. **Set Up Billing**: – Add a billing account to unlock free-tier credits and API functionality. 4. **Create Service Account Credentials**: – Go to **Credentials** – **Create Credentials** – **Service Account Key**. – Choose your project and download the JSON key file. — #### 2. **Install the Required Python Library** Install the `google-cloud-texttospeech` library to interact with the API: “`bash
pip install google-cloud-texttospeech
“` — #### 3. **Write the Python Code** Here’s a sample Python script to convert text into speech: “`python
from google.cloud import texttospeech
import os def text_to_speech(text, output_filename): # Set the path to your service account key file os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_service_account_key.json" # Initialize the client client = texttospeech.TextToSpeechClient() # Set the text input synthesis_input = texttospeech.SynthesisInput(text=text) # Configure voice parameters voice = texttospeech.VoiceSelectionParams( language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL ) # Set audio configuration audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.MP3 ) # Generate speech response = client.synthesize_speech( input=synthesis_input, voice=voice, audio_config=audio_config ) # Save the audio file with open(output_filename, "wb") as out: out.write(response.audio_content) print(f"Audio content written to file: {output_filename}") # Example usage
if __name__ == "__main__": text = "Hello, this is a sample text converted into speech using Google Text-to-Speech API." output_file = "output.mp3" text_to_speech(text, output_file)
“` — #### 4. **Run the Script** Save the script and run it in the terminal: “`bash
python text_to_speech.py
“` — #### 5. **Listen to the Audio** – The output will be saved as an `.mp3` file (e.g., `output.mp3`). – Open it with your media player to hear the synthesized speech. — ### Key Features of Google Text-to-Speech API – **High-Quality Voices**: Supports multiple languages, accents, and genders. – **SSML Support**: Customize speech with pauses, emphasis, and more using SSML. – **Flexible Audio Output**: Supports MP3, OGG, and LINEAR16 formats. – **Wide Language Coverage**: Over 40 languages and variants available. — Start building lifelike voice applications today with Google Text-to-Speech API! If this tutorial helped you, like, share, and subscribe for more. #GoogleTextToSpeech #PythonCode #GCP #CloudTextToSpeech #TTSAPI #GoogleCloudPlatform