傻瓜讀書軟件完成版TK代碼(功能滿足,但不完美)
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 29 11:01:21 2020
@author: liyan
"""
import importlib,sys
importlib.reload(sys)
import tkinter
from tkinter.filedialog import askopenfilename
from docx import Document
from docx.shared import Inches
import pyttsx3
from tkinter import *
from tkinter import ttk
import os
import pyaudio
import time
import threading
import wave
class Recorder():
? ? def __init__(self, chunk=1024, channels=1, rate=64000):
? ? ? ? self.CHUNK = chunk
? ? ? ? self.FORMAT = pyaudio.paInt16
? ? ? ? self.CHANNELS = channels
? ? ? ? self.RATE = rate
? ? ? ? self._running = True
? ? ? ? self._frames = []
? ? def start(self):
? ? ? ? threading._start_new_thread(self.__recording, ())
? ? def __recording(self):
? ? ? ? self._running = True
? ? ? ? self._frames = []
? ? ? ? p = pyaudio.PyAudio()
? ? ? ? stream = p.open(format=self.FORMAT,
? ? ? ? ? ? ? ? ? ? ? ? channels=self.CHANNELS,
? ? ? ? ? ? ? ? ? ? ? ? rate=self.RATE,
? ? ? ? ? ? ? ? ? ? ? ? input=True,
? ? ? ? ? ? ? ? ? ? ? ? frames_per_buffer=self.CHUNK)
? ? ? ? while(self._running):
? ? ? ? ? ? data = stream.read(self.CHUNK)
? ? ? ? ? ? self._frames.append(data)
?
? ? ? ? stream.stop_stream()
? ? ? ? stream.close()
? ? ? ? p.terminate()
?
? ? def stop(self):
? ? ? ? self._running = False
?
? ? def save(self, filename):
? ? ? ??
? ? ? ? p = pyaudio.PyAudio()
? ? ? ? if not filename.endswith(".wav"):
? ? ? ? ? ? filename = filename + ".wav"
? ? ? ? wf = wave.open(filename, 'wb')
? ? ? ? wf.setnchannels(self.CHANNELS)
? ? ? ? wf.setsampwidth(p.get_sample_size(self.FORMAT))
? ? ? ? wf.setframerate(self.RATE)
? ? ? ? wf.writeframes(b''.join(self._frames))
? ? ? ? wf.close()
? ? ? ? print("Saved")
l=[]
def openfile():
? ? global file_path,l
? ? file_path=askopenfilename() ?
? ??
? ? if os.path.splitext(str(file_path))[1]=='.txt': ?###對于TXT文件的處理
? ? ? ? f=open(file_path,encoding='utf-8')
? ? ? ? fread=f.read()
? ? ? ? f=str(fread)
? ? ? ? text.insert(END,f) ??
? ? elif os.path.splitext(str(file_path))[1]=='.docx': ###對于WORD文件處理
? ? ? ? f=Document(file_path)
? ? ? ? for para in f.paragraphs:
? ? ? ? ? ? l.append(para.text)
? ? ? ? text.insert(END,str(l))
def cleartext():
? ? text.delete('1.0','end')
def readbook():
? ? global l
? ? l=text.get('0.0', 'end')
? ? engine=pyttsx3.init()
? ? engine.say(str(l))
? ? engine.runAndWait()
? ??
? ??
? ??
def readtext():
? ? global l,rec
? ? l=text.get('0.0', 'end')
? ? strtime=time.strftime("%Y%m%d%H%M%S", time.localtime())
? ? name=strtime+".wav"
? ? rec = Recorder()
? ? begin = time.time()
? ? rec.start()
? ? print("Start recording")
? ? engine=pyttsx3.init()
? ? engine.say(str(l))
? ? engine.runAndWait()
? ? print("Stop recording")
? ? rec.stop()
? ? fina = time.time()
? ? rec.save(name)
if __name__=='__main__':
? ? root=Tk()
? ? root.title('讀書軟件')
? ? root.geometry("720x480")
? ? root.configure(bg="white")
? ? text=tkinter.Text(root,bg="yellow")
? ? text.grid(row=2,columnspan=4,padx=20,pady=20)?
? ? Button1=tkinter.Button(root, text ="選擇文件", command = openfile)
? ? Button1.grid(row=0,column=0,padx=20,pady=20)
? ? Button2=tkinter.Button(root, text ="清空內(nèi)容", command = cleartext)
? ? Button2.grid(row=0,column=1,padx=20,pady=20)
? ? Button3=tkinter.Button(root, text ="語音讀書", command = readbook)
? ? Button3.grid(row=0,column=2,padx=20,pady=20)
? ? Button4=tkinter.Button(root, text ="錄音輸出", command = readtext)
? ? Button4.grid(row=0,column=3,padx=20,pady=20)
? ? root.mainloop()