0

Dear All,

I wrote a simple program of data entry into excel using python. But I task is that this manual entry will be performed from user voice input.

Can anybody help me in this regard.

import PySimpleGUI as sg
import pandas as pd
import speech_recognition as sr



# Add some color to the window
sg.theme('Green')

exfile = 'Break Down Report.xlsx'
df = pd.read_excel(exfile)

layout = [ # for input text
    [sg.Text('Machines Breakdown Entries Table:')],
    [sg.Text('S.No', size=(15,1)), sg.InputText(key='S.No')],
    [sg.Text('Fault Desc', size=(15,1)), sg.InputText(key='Fault Desc')],
    [sg.Text('Start Time', size=(15,1)), sg.InputText(key='Start Time')],
    [sg.Text('End Time', size=(15,1)), sg.InputText(key='End Time')],
    [sg.Text('Date', size=(15,1)), sg.InputText(key='Date')],
    [sg.Text('Employe Name', size=(15,1)), sg.InputText(key='Employe Name')],
    # for buttons
    [sg.Button('Speak'), sg.Save(), sg.Button('Clear'), sg.Quit()]
]

# main tool bar Window text
window = sg.Window('Daily Breakdown Report ', layout)

def clear_input():
    for key in values:
        window[key]('')
    return None

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Quit':
        break
    if event == 'Clear':
        clear_input()
    if event == 'Save':
        df = df.append(values, ignore_index=True)
        df.to_excel(exfile, index=False)
        sg.popup('Data saved!')
        clear_input()
window.close()

I want to develop a program in Python that performs voice activity in an excel sheet, similar to Google Docs.

According to above code. Manual entries will be performed by the user typing. But i want create a voice button in which user will command by voice and input should store into text and save in excel sheet.

1 Answers1

0

Using thread to do the conversion, like the method window.perform_long_operation.

Demo Code

import speech_recognition as sr
import PySimpleGUI as sg

def speak():
    while True:
        r = sr.Recognizer()
        text = '~ Not recongnized ~'
        with sr.Microphone() as source:
            audio = r.listen(source)
            try:
                text = r.recognize_google(audio,language="en-IN")
            except:
                pass
            return text

layout = [
    [sg.Multiline(size=(80, 10), autoscroll=True, key='-ML-')],
    [sg.Push(), sg.Button('Speak')],
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Speak':
        window['Speak'].update(disabled=True)
        window.perform_long_operation(speak, 'Done')
    elif event == 'Done':
        text = values['Done']
        window['Speak'].update(disabled=False)
        window['-ML-'].update(text+'\n', append=True)

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • How can I add the voice input text in an excel file? – Hassaan Rasheed Oct 27 '22 at 07:50
  • It depend on what format you want it saved to an excel file. You can find the answer about how to save something to an excel file by yourself. – Jason Yang Oct 27 '22 at 09:35
  • I know how to save the user input into excel by following this library "import pandas as pd" and I defined a variable this "exfile = 'Break Down Report.xlsx' df = pd.read_excel(exfile)" ...... I am unable to extract voice text into excel. :-( – Hassaan Rasheed Oct 27 '22 at 09:39
  • I cannot help about it if I don't understand what it mean, you already get the voice text in the variable `text` under event `'Done'`. – Jason Yang Oct 27 '22 at 11:10