Sunday, 17 March 2019

Python code to Read the data in first column in excel file (Text to speech using pyttsx )

import pyttsx
import openpyxl
import time


filename=str(raw_input('enter path to excel file: ')) # enter the complete path of excel file
wb = openpyxl.load_workbook(filename)
ws = wb.get_sheet_by_name('Sheet1') # gets Sheet1 in excel
rowcount=ws.max_row

def say(s):
       engine = pyttsx.init()
       engine.setProperty('volume', 0.2)
       engine.setProperty('rate', 100)
       voices= engine.getProperty('voices')                                                               
       engine.setProperty('voice', 'english-us')                 
       engine.say(s)
       a = engine.runAndWait()

if __name__== "__main__":
       for i in range(rowcount):              # loop through entire rows in excel
               mytext = str(ws.cell(row=i+1,column=1).value) # gets the first column data
               say(mytext)


Usage:
Create an excel file and write any text in the first column as shown below and save it. In this example, i have saved the file in Desktop with the name SEC8


Once u run the python code, you will be asked to
Enter the path where the excel file is
here i have saved the excel file at C:\Users\SARVANI\Desktop\SEC8.xlsx
The excel file name is SEC8.xlsx




No comments:

Post a Comment