欢迎光临散文网 会员登陆 & 注册

傻瓜读书软件在升级(III)

2020-03-04 20:43 作者:一心想当网红的李老师  | 我要投稿

这次案件只有一个了。

再大神的指导下,成功分析了文件类型,然后选择了合适的处理方式。目前可以处理txt和word文件。感觉又有了提高。

代码如下:(python3.7版本)

#! /usr/bin/env python

# -*- coding: utf-8 -*-

import importlib,sys

importlib.reload(sys)


import tkinter

from tkinter.filedialog import askopenfilename

from docx import Document

from docx.shared import Inches

import pyttsx3

import os


l=[]

def openfile():

    file_path=askopenfilename()

    if os.path.splitext(str(file_path))[1]=='.txt':    ###利用os模块的功能,成功分解文件的后缀

        f=open(file_path,encoding='utf-8')

        fread=f.read()

        f=str(fread)

        engine=pyttsx3.init()

        engine.say(f)

        engine.runAndWait()

    elif os.path.splitext(str(file_path))[1]=='.docx':

        f=Document(file_path)

        for para in f.paragraphs:

            l.append(para.text)

        engine=pyttsx3.init()

        engine.say(str(l))

        engine.runAndWait()



top=tkinter.Tk()

Button1=tkinter.Button(top, text ="选择文件", command = openfile)

Button1.pack()

top.mainloop()

虽然不是非常现代化,但是更加进步了一下。后续继续升级改造。

傻瓜读书软件在升级(III)的评论 (共 条)

分享到微博请遵守国家法律