windows文本轉語音 通過java 調用python 生成exe可執行文件一條龍
我已記不清 我失敗過多少次 ,找過多少資料 ,但是功夫不負有心人 ,還是成功了。 所有資料和需要的語音模組的資料以放置在文章末尾, 有些是引用別人的部落格的部分內容, 原文是在有道雲筆記,所以沒有圖,請見諒。
官網下載
//www.python.org/downloads/windows/
安裝
雙擊exe
驗證
win+r 輸入 cmd 打開窗口輸入 python
輸入: python
C:\Users\Administrator>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包,地址是
[//pypi.python.org/pypi/pip#downloads](//note.youdao.com/)
下載完成之後,解壓到一個文件夾,用cmd進入這個解壓好的文件夾,輸入:
Python setup.py install
Python setup.py install
running install
running bdist_egg
running egg_infoile_cache.py -> build\bdist.win-amd64\egg\pip\_vendor\cachecontrol\caches
copying build\lib\pip\_vendor\cachecontrol\caches\redis_cache.py -> build\bdist.win-amd64\egg\pip\_vendor\cachecontrol\caches
....bulid 問價 和複製文件操作
Adding pip 20.1.1 to easy-install.pth file
Installing pip-script.py script to E:\Program Files\python\Scripts
Installing pip.exe script to E:\Program Files\python\Scripts
Installing pip3-script.py script to E:\Program Files\python\Scripts
Installing pip3.exe script to E:\Program Files\python\Scripts
Installing pip3.7-script.py script to E:\Program Files\python\Scripts
Installing pip3.7.exe script to E:\Program Files\python\Scripts
### 生成成功
Installed e:\program files\python\lib\site-packages\pip-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顯示不是內部命令的情況分為兩種:
- 系統環境變數中沒用添加你的Python所在的文件夾:例如C:\python36\Scripts;
- 其他軟體中也有pip,系統無法識別是哪個,你可以用命令where pip查詢pip
安裝 語音模組
在窗口中
輸入
pip3 install -i //pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
執行結果
E:\Program Files\python\dist\pip-20.1.1>pip3 install -i //pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
Looking in indexes: //pypi.tuna.tsinghua.edu.cn/simple/
Collecting pyttsx3
Downloading //pypi.tuna.tsinghua.edu.cn/packages/fd/4b/4981cc11f1a288fa44fb906e07786eaa081b1b42da3f6dce38fe16af0d99/pyttsx3-2.87-py3-none-any.whl (39 kB)
Collecting comtypes; platform_system == "Windows"
Downloading //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 Files\python\dist\pip-20.1.1>pip3 install -i //pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
Looking in indexes: //pypi.tuna.tsinghua.edu.cn/simple/
Requirement already satisfied: pyttsx3 in e:\program files\python\lib\site-packages (2.87)
Requirement already satisfied: comtypes; platform_system == "Windows" in e:\program files\python\lib\site-packages (from pyttsx3) (1.1.7)
E:\Program Files\python\dist\pip-20.1.1>
py3 安裝pyinstaller 生成exe
pip3 install -i //pypi.tuna.tsinghua.edu.cn/simple/ pyttsx3
為什麼要生成可執行文件
- 不需要安裝對應的編程環境
- 可以將你的應用閉源
- 用戶可以方便、快捷的直接使用
打包工具
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文件,下載地址:
//github.com/pyinstaller/pyinstaller
點擊Clone or download,然後選擇Download ZIP下載相應壓縮包,解壓得到pyinstaller文件
在cmd中進入pyinstaller-develop\PyInstaller 文件夾下,使用
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 -i favicon.ico nhdz.py
//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
(也許還有,但是我已經安裝成功了…)
上述所有第三方庫的安裝程式都可以在//www.lfd.uci.edu/~gohlke/pythonlibs/找到,例如
找了半天缺少一個組件這個需要和安裝的 python 版本配合 我的python 3.7 X64
pywin32-227-cp37-cp37m-win_amd64.whl
//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: [email protected]
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
會生成exe 文件
exe 文件在 所在目錄的 dist下面
以上的版本鏈接 都在我百度網盤中
鏈接://pan.baidu.com/s/1QK0w-I39n-h6pvctHMSFGA
提取碼:14wk
複製這段內容後打開百度網盤手機App,操作更方便哦