zoukankan      html  css  js  c++  java
  • windows文本转语音 通过java 调用python 生成exe可执行文件一条龙

    所有资料和需要的语音模块的资料以放置在文章末尾, 有些是引用别人的博客的部分内容, 原文是在有道云笔记,所以没有图 。

    有道云笔记 http://note.youdao.com/noteshare?id=3305b28982db27b84f86e7a267132be6

    好东西需分享 , 路过的点个赞。

    官网下载

    https://www.python.org/downloads/windows/
    

    安装

    双击exe 
    

    验证

    win+r 输入 cmd 打开窗口输入 python

    输入: python 
    
    
    
    C:UsersAdministrator>python
    Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    安装python pip3 安装pip环境

    官网下载pip包,地址是

    
    [https://pypi.python.org/pypi/pip#downloads](https://note.youdao.com/)
    
    

    下载完成之后,解压到一个文件夹,用cmd进入这个解压好的文件夹,输入:

    Python setup.py install
    

    以下是执行结果:

    Python setup.py install
    running install
    running bdist_egg
    running egg_infoile_cache.py -> builddist.win-amd64eggpip\_vendorcachecontrolcaches
    copying buildlibpip\_vendorcachecontrolcaches
    edis_cache.py -> builddist.win-amd64eggpip\_vendorcachecontrolcaches
    ....bulid 问价 和复制文件操作
    Adding pip 20.1.1 to easy-install.pth file
    Installing pip-script.py script to E:Program FilespythonScripts
    Installing pip.exe script to E:Program FilespythonScripts
    Installing pip3-script.py script to E:Program FilespythonScripts
    Installing pip3.exe script to E:Program FilespythonScripts
    Installing pip3.7-script.py script to E:Program FilespythonScripts
    Installing pip3.7.exe script to E:Program FilespythonScripts
    
    ### 生成成功
    Installed e:program filespythonlibsite-packagespip-20.1.1-py3.7.egg
    Processing dependencies for pip==20.1.1
    Finished processing dependencies for pip==20.1.1
    
    

    可检查 pip 是否成功安装

    cmd 打开窗口 输入 pip
    或者输入 pip3

    安装pip后,cmd输入pip显示不是内部命令的情况分为两种:

    1. 系统环境变量中没用添加你的Python所在的文件夹:例如E:Program Filespython;
    2. 其他软件中也有pip,系统无法识别是哪个,你可以用命令where pip查询pip
    
    

    安装pyttsx3 语音模块

    在窗口中
    输入

    pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
    

    执行结果

    
    E:Program Filespythondistpip-20.1.1>pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
    Collecting pyttsx3
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fd/4b/4981cc11f1a288fa44fb906e07786eaa081b1b42da3f6dce38fe16af0d99/pyttsx3-2.87-py3-none-any.whl (39 kB)
    Collecting comtypes; platform_system == "Windows"
      Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fb/b8/f8aa21774acb4535e32f6a89055876ca497ff806f9b1b1912b469284a61e/comtypes-1.1.7.zip (180 kB)
         |████████████████████████████████| 180 kB 273 kB/s
    Using legacy setup.py install for comtypes, since package 'wheel' is not installed.
    Installing collected packages: comtypes, pyttsx3
        Running setup.py install for comtypes ... done
    Successfully installed comtypes-1.1.7 pyttsx3-2.87
    
    E:Program Filespythondistpip-20.1.1>pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
    Requirement already satisfied: pyttsx3 in e:program filespythonlibsite-packages (2.87)
    Requirement already satisfied: comtypes; platform_system == "Windows" in e:program filespythonlibsite-packages (from pyttsx3) (1.1.7)
    
    E:Program Filespythondistpip-20.1.1>
    

    py3 安装pyinstaller 生成exe

     pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
    

    为什么要生成可执行文件

    1. 不需要安装对应的编程环境
    2. 可以将你的应用闭源
    3. 用户可以方便、快捷的直接使用
      打包工具
    pyinstaller
    

    安装pyinstaller

    如果你的网络稳定,通常直接使用下面的命令安装即可:

    pip install pyinstaller
    或者
    pip3 install pyinstaller
    

    pyinstaller 安装过程
    python3.7似乎还没有匹配的pyinstaller版本,直接使用

    pip install pyinstaller
    

    命令行会报错:

    python -m pip install --upgrade pip
    

    windows下 pip3 install pyinstaller 总是失败,

    pip3 install pyinstaller
    Collecting pyinstaller
      Downloading PyInstaller-3.6.tar.gz (3.5 MB)
         |▌                               | 61 kB 10 kB/s eta 0:05:38                                                   
    

    改用:

    在github上下载pyinstaller文件,下载地址:

    https://github.com/pyinstaller/pyinstaller
    

    点击Clone or download,然后选择Download ZIP下载相应压缩包,解压得到pyinstaller文件

    在cmd中进入pyinstaller-developPyInstaller 文件夹下,使用

    python setup.py build
    和
    python setup.py install
    

    命令安装pyinstaller库,等待安装完成,当出现

    Finished processing dependencies for PyInstaller==4.0.dev0+a1f92c6a08
    

    即表示安装成功

    检查是否安装成功

    192:~ wxs$ pyinstaller -v
    3.6
    

    pyinstaller参数作用

    • -F 表示生成单个可执行文件
    • -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
    • -w 表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧
    • -c –console, –nowindowed 使用控制台,无界面(默认)
    • -p 表示你自己自定义需要加载的类路径,一般情况下用不到
    • -i 表示可执行文件的图标

    其他参数,可以通过pyinstaller --help查看

    开始打包

    进入python需要打包的脚本所在目录,然后执行下面的命令即可:

    python -F  test.py
    

    python3 pyttsx3 不能运行 缺少组件

    这是参考的博客

    https://blog.csdn.net/sxt1001/article/details/102991306?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
    

    还有部分依赖包,经过反复尝试,发现需要如下依赖包:

    pywin32_ctypes
    pefile
    future
    altgraph
    

    找了半天缺少一个组件这个需要和安装的 python 版本配合 我的python 3.7 X64

    pywin32-227-cp37-cp37m-win_amd64.whl
    

    所有的下载可能比较慢 建议复制链接添加到迅雷 可下载 ,不过我给了链接,就不用了担心了哈。

    (也许还有... 但是我已经安装成功了...)
    可根据运行python install PyInstaller 文件报错找到未下载的文件来安装文件
    上述所有第三方库的安装程序都可以在https://www.lfd.uci.edu/~gohlke/pythonlibs/找到,例如

    对于安装whl格式的文件,首先要安装对应 python版本 wheel包
    利用 pip install wheel或下载再安装:

    下载地址: https://pypi.python.org/pypi/wheel

    解压后 cd 目录中
    python setup.py install

    比如安装
    pywin32-227-cp37-cp37m-win_amd64.whl

    就是 pip intall pywin32-227-cp37-cp37m-win_amd64.whl

    pyttsx3 用法:

    https://pyttsx3.readthedocs.io/en/latest/engine.html
    

    附上源码:

    #! /usr/bin/env python3
    # -- coding: UTF-8 --
    # @Time    : 2020-05-28  22:14
    # @Author  : wxs
    # @FileName: testSpeak2.py
    # @Software: IntelliJ IDEA
    # @User : wxs
    # @E-mial: 1371224781@qq.com
    
    import pyttsx3.drivers
    
    import sys
    import os
    curPath = os.path.abspath(os.path.dirname(__file__))
    rootPath = os.path.split(curPath)[0]
    sys.path.append(rootPath)
    pythoncom.CoInitialize()
    abs_file =__file__
    
    print(abs_file)
    with open('D:\pythonPprojct\read.txt','r',encoding='utf-8') as f:
        line = f.read()
        #文件不大,一次性读取
    
        engine = pyttsx3.init("sapi5")
        rate = engine.getProperty('rate')
        engine.setProperty('rate', rate-100)
        volume = engine.getProperty('volume')
        engine.setProperty('volume', volume+0.75)
        # engine.iterate() must be called inside externalLoop()
        engine.say(line)
        engine.runAndWait()
    
    
    

    read.txt

    君不见,黄河之水天上来,奔流到海不复回。君不见,高堂明镜悲白发,朝如青丝暮成雪。人生得意须尽欢,莫使金樽空对月。天生我材必有用,千金散尽还复来。烹羊宰牛且为乐,会须一饮三百杯。岑夫子,丹丘生,将进酒,杯莫停
    

    java 调用 python

    
    package test;
    /*
    键盘
    
    * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
    * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
    * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
    * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
    * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
    * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
    * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ |  │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
    * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
    * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
    * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
    * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
    * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
    * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
    * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
    * 
    * Code is far away from bug with the keyboard protecting.
    */
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     * Copyright (C), 2020-2020
     *
     * @Author 嗜学劣人
     * @Date: 2020/5/30 19:35
     * @FileName: PythonSpeak
     * @Description: java调用Python示例
     */
    public class PythonSpeak {
        public static void main(String[] args) {
    
    
            Process proc;
            try {
                proc = Runtime.getRuntime().exec("E:\Program Files\python\python.exe D:\pythonPprojct\testSpeak2.py");
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    

    cd testSpeak2.py 所在目录

    pyinstaller -F -w testSpeak2.py
    

    pyinstaller -F -w testSpeak2.py 参数最好不加 -w

    会生成exe 文件

    exe 文件在 所在目录的 dist下面

    以上的版本链接 都在我百度网盘中

    链接:https://pan.baidu.com/s/1QK0w-I39n-h6pvctHMSFGA
    提取码:14wk
    复制这段内容后打开百度网盘手机App,操作更方便哦

  • 相关阅读:
    上传文件大小问题
    中检测到有潜在危险的 Request.Form 值
    Session
    C#浏览器中在线操作文档
    数据导入
    文件上传(插件版)和下载
    Leetcode练习(Python):递归类:面试题10- I. 斐波那契数列:写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项。斐波那契数列的定义如下: F(0) = 0,&#160; &#160;F(1)&#160;= 1 F(N) = F(N
    Leetcode练习(Python):递归类:面试题07. 重建二叉树:输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
    Leetcode练习(Python):递归类:面试题 16.11. 跳水板:你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。 返回的长度需要从小到大排列。
    Leetcode练习(Python):递归类:面试题 08.06. 汉诺塔问题:在经典汉诺塔问题中,有 3 根柱子及 N 个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子。一开始,所有盘子自上而下按升序依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面)。
  • 原文地址:https://www.cnblogs.com/wxs121/p/12994584.html
Copyright © 2011-2022 走看看