Python Forum
reading text file with gtts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: reading text file with gtts (/thread-30487.html)



reading text file with gtts - Nickd12 - Oct-22-2020

i am looking to retrieve contents that are in a file i am able to print the contents that i search but i am not able to use gtts to speak it.

def record_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print('listening:')
        r.pause_threshold = .7
        r.energy_threshold = 4000
        r.dynamic_energy_adjustment_damping = True
        audio = r.listen(source)
    data = ''  # the audio that is said
    try:
        data = r.recognize_google(audio)
        print('you said: ' + data)  # 'you said' + the data that was said
    except sr.UnknownValueError:
        print('google speech recognition could not understand the audio, unknown error')
    except sr.RequestError as e:
        print('request results from google speech recognition service error' + e)
    except LookupError:  # speech is unintelligible
        print("Could not understand audio")
    return data


def assistant_response(text):
    print(text)
    tts = gTTS(text=text, lang="en-US", slow=False, )
    filename = "voice.mp3"
    tts.save(filename)
    playsound(filename, True)
    os.remove(filename)

"""==============================================================================================================="""

"""File location information"""
read_appointment_file_location = r"My_appointments"
write_appointment_file_location = r"My_appointments"
is_file_appointment_empty_location = r"My_appointments"




class Cal:
    def __init__(self, date, time, appointment):
        self.date = date
        self.time = time
        self.appointment = appointment


    def __str__(self):
        return "Date: {0}\n Time: {1}\n Appointment: {2}".format(self.date, self.time, self.appointment)

    def date(self):
        return_date = self.date
        return return_date


def search_appointment():

    # search_name=input("Enter the name\n")
    appointment_book_file = open(read_appointment_file_location, mode="rb")
    search_date = get_date()
    is_appointment_found = False
    list_appointment = pickle.load(appointment_book_file)
    for each_appointment in list_appointment:
        appointment_date = each_appointment.date
        search_date = search_date.lower()
        appointment_date = appointment_date.lower()
        if appointment_date == search_date:
            print(each_appointment)
            assistant_response(each_appointment)
            is_appointment_found = True

    if not is_appointment_found:
        print("No appointment found with the provided search date")

    return


def suffix(d):
    return 'th' if 11 <= d <= 13 else {1: 'st', 2: 'nd',3:'rd'}.get(d % 10, 'th')


def custom_strftime(format, t):
    return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))


def get_date():
    my_date = datetime.datetime.today()
    return custom_strftime('%B {S} %Y', datetime.datetime.now())
   # return calendar.day_name[my_date.weekday()] + " " + custom_strftime('%B {S}, %Y', datetime.datetime.now())

search_appointment()