PYTHON script

Hello Gentlemen,
I am testing the GTTS function IN PYTHON , CGI ENVIRONMENT , with the
aim of implementing it in browsers that do not support text-to-speech.
I can turn text to speech in MICROSOFT EDGE AND GOOGLE CHROME
browsers with the speechSynthesis function.
This function acts on error messages ( for example ) , system caption (
onmouseover ) , etc.
In OPERA , VIVALD , FIREFOX browsers, the speechSynthesis function is
not working.
The PYTHON script to convert text to speech is very simple:

from gtts import gTTS
from gtts.tokenizer.pre_processors import abbreviations, end_of_line
from time import sleep
from datetime import datetime
import os
import pyglet
import cgi

Create the text

form = cgi.FieldStorage()
idioma = form.getvalue(‘idioma’)
texto = form.getvalue(‘texto’)
date_s = (‘%Y%m%d%H%M%S%f’))

tts = gTTS(text=texto, lang=idioma , slow=False, pre_processor_funcs = [abbreviations, end_of_line])
filename = ‘c:/payback/temp/’ + date_s + ‘.mp3’
if os.path.exists(filename): os.remove(filename) #remove temperory file

music =, streaming=False)

sleep(music.duration) #prevent from killing
os.remove(filename) #remove temperory file
#print (“Content-Type: text/html\n\n”)

This script is run from the URL:
https://server_domain/ gentlemen, let’s discuss our strategic planning for the year 2024

My problem is in the last line of the script ( print (“Content-Type: text/html\n\n”) that is, if you don’t include it, the script plays the created mp3 file (so it converted text into voice) however it sends an error message: Internal Server Error ( Premature end of script headers: )
If you include it, the script plays the mp3 file (no error) however it sends a new empty page harming the systematics of my need ( speak an error message , speak a caption with the onmouseover event , etc )
no pagination.

I’m researching the CALLBACK function in javascript to try to work around this problem.This function would call the URL and when returning it would execute a BACK(-1) function.
Note - Doesn’t seem like a good solution.
Does anyone know how to solve this problem more properly?
Thank you for your attention,
Note - This text was translated by google.

To create the specified text in JavaScript, you can use the XMLHttpRequest or the newer fetch API to send a request to the server-side script that handles the form submission. Here’s an example of how you can achieve this:

htmlCopy code

<!DOCTYPE html>
  <title>Form Submission Example</title>

  <h1>Form Submission Example</h1>

  <form id="myForm">
    <label for="idioma">Idioma:</label>
    <input type="text" name="idioma" id="idioma"><br>

    <label for="texto">Texto:</label>
    <input type="text" name="texto" id="texto"><br>

    <button type="submit">Submit</button>

    document.getElementById('myForm').addEventListener('submit', function(event) {
      event.preventDefault(); // Prevent form submission

      var form = new FormData(; // Get form data
      var xhr = new XMLHttpRequest(); // Create XMLHttpRequest object

      // Set up the request'POST', '/path/to/your/server-side/script', true);
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

      // Handle the response
      xhr.onload = function() {
        if (xhr.status === 200) {
          console.log(xhr.responseText); // Do something with the response
        } else {
          console.error('Request failed. Status: ' + xhr.status);

      // Send the request


In this example, the JavaScript code sets up an event listener on the form submission. When the form is submitted, it prevents the default form submission behavior, creates an XMLHttpRequest object, and sends the form data to the server-side script specified by the open method. Make sure to replace /path/to/your/server-side/script with the actual path to your server-side script that handles the form submission.

On the server-side, you would need to use a server-side language like Python to handle the form data and perform the necessary operations. Here’s a Python example using the CGI module:

pythonCopy code

import cgi
import os
from datetime import datetime
from gtts import gTTS
import pyglet

form = cgi.FieldStorage()
idioma = form.getvalue('idioma')
texto = form.getvalue('texto')
date_s ='%Y%m%d%H%M%S%f')

tts = gTTS(text=texto, lang=idioma, slow=False)
filename = 'c:/payback/temp/' + date_s + '.mp3'
if os.path.exists(filename):
    os.remove(filename)  # remove temporary file

music =, streaming=False)

Please note that this is a simplified example, and you may need to modify the server-side code based on your specific requirements and server setup. Additionally, make sure you have the necessary dependencies (gtts and pyglet) installed for the server-side code to work properly.

Thank you for your kindness in replying.
I tested with the suggested HTML and got the following message (in apache)
Premature end of script headers:, referer: https://server/development/test/tts.html
There was no sound reproduction.
Note - This text was translated by google.