Python Forum
Is it possible for a Bot to read out the information on the screen aloud?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible for a Bot to read out the information on the screen aloud?
#1
Hey pyhton newbie here -

Question:


I am developing a Python voice assistant for visually impaired users to help them search the web and gain information using voice commands. The assistant can currently perform searches, but I am struggling with making it read the content of web pages aloud.

Problem:

I need guidance on how to:

Open websites and extract text content from the web pages.

Read the extracted text content aloud using the existing pyttsx3 setup.

I haven't implemented any solution for this part yet, as I am unsure about the best approach or what additional libraries or tools are required.

What I've Tried:

I have researched libraries like BeautifulSoup for web scraping and libraries like requests to open web pages, but I am unsure how to integrate these with my current voice assistant setup.

What I'm Seeking:

Recommendations on how to open websites and extract text content using Python libraries.

Integration of the extracted content with pyttsx3 to read it aloud.

Code snippets or examples that could help with this implementation.

Additional Details:


Current Setup:
I have a basic Python script using pyttsx3 for text-to-speech conversion.

Research: I have looked into BeautifulSoup for web scraping and requests for opening web pages but haven't figured out how to effectively use them in this context.

Issues: I am not sure how to proceed with opening web pages, extracting relevant text, and then having my voice assistant read it aloud.

Expectations vs. Reality:

Expected: The assistant should read out relevant text content from a web page.

Actual: I am currently unable to open web pages and extract the text content to read aloud.

code -
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
import time

print('Loading Sonic Surfing...')

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

def speak(text):
    engine.say(text)
    engine.runAndWait()

def wishMe():
    hour = datetime.datetime.now().hour
    if 0 <= hour < 12:
        speak("Good Morning!")
    elif 12 <= hour < 18:
        speak("Good Afternoon!")
    else:
        speak("Good Evening!")

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

        try:
            statement = r.recognize_google(audio, language='en-in')
            print(f"user said: {statement}\n")
        except Exception as e:
            speak("I didn't hear you, please say that again")
            return "none"
        return statement.lower()

speak("Loading Sonic Surfing")
wishMe()

def get_weather():
    # Simulated weather conditions based on time of day
    hour = datetime.datetime.now().hour
    if 6 <= hour < 12:
        weather_desc = "sunny"
    elif 12 <= hour < 18:
        weather_desc = "cloudy"
    else:
        weather_desc = "clear"
    return weather_desc

if __name__ == '__main__':
    while True:
        speak("What can I do for you?")
        statement = takeCommand()
        if statement == "none":
            continue

        if "exit" in statement or "goodbye" in statement or "turn off" in statement:
            speak('Turning off Sonic Surfing.')
            break

        elif 'open youtube' in statement:
            webbrowser.open_new_tab("https://youtube.com")
            speak("YouTube is now open")
            time.sleep(3)

        elif 'search orange juice gaming' in statement:
            webbrowser.open_new_tab("https://www.youtube.com/results?search_query=Orange+Juice+Gaming")
            speak("Searching for Orange Juice Gaming on YouTube")
            time.sleep(3)

        elif 'open google' in statement:
            webbrowser.open_new_tab("https://www.google.com")
            speak("Google Chrome is open now")
            time.sleep(5)

        elif 'open gmail' in statement:
            webbrowser.open_new_tab("https://mail.google.com")
            speak("Google Mail is open now")
            time.sleep(5)

        elif 'what is the time' in statement or 'tell me the time' in statement or 'current time' in statement:
            current_time = datetime.datetime.now().strftime("%H:%M")
            speak(f"The time is {current_time}")

        elif 'search' in statement:
            search_query = statement.replace("search", "").strip()
            if search_query:
                webbrowser.open_new_tab(f"https://www.google.com/search?q={search_query}")
                speak(f"Searching for {search_query} on Google")
                time.sleep(5)

        # Code for Google search
        elif 'search' in statement and 'google' in statement:
            search_query = statement.replace("search", "").replace("google", "").strip()
            if search_query:
                webbrowser.open_new_tab(f"https://www.google.com/search?q={search_query}")
                speak(f"Searching for {search_query} on Google")
                time.sleep(5)
        
        elif 'what is the weather' in statement or 'weather' in statement:
            weather_desc = get_weather()
            speak(f"The weather is {weather_desc}.")

        # Open Wikipedia website
        elif 'open wikipedia' in statement:
            webbrowser.open_new_tab("https://en.wikipedia.org")
            speak("Wikipedia is now open")
Gribouillis write Jun-19-2024, 09:26 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Hi Friend!

Perhaps, start solving it step by step.

Quote:I am currently unable to open web pages and extract the text content to read aloud.

there are at least three steps - open web page, extract the text, read it

it is hard to figure out which of them (or all) you fail.

Settle on some specific page for your further exercise, write the very small code to open it, come back with it - either it works or not.

It seems you have less or more idea about instruments to use, but suffer from lack of strategy. Try to do this methodically.

And avoid double-posting, I think it is not welcome :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I dont understand on how to create a Bot that can read info on the screen aloud RaySuS777 0 167 Jun-19-2024, 10:02 AM
Last Post: RaySuS777
  Bot that can read info on the screen aloud... RaySuS777 2 286 Jun-18-2024, 05:52 PM
Last Post: RaySuS777
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,319 Feb-06-2019, 01:25 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020