[Details] Use Python's pyttsx3, pygame and PySimpleGUI libraries to implement text-to-speech tools (no network interface required)

Table of contents

Preface

1. What is pyttsx3?

2. Install pyttsx3

3. Check the voice package

4. Add voice package

1. Download the voice pack

2. Install voice pack

5. Function implementation and resource download

1. Download the required libraries

2. Runtime interface

3. Resource download


Preface

I look forward to the beauty of the mountains and rivers thousands of miles away, and praise the eternal spring of the motherland. The National Day is approaching, first of all I wish you a happy National Day! Every time I look for information on the Internet, I have to read "long articles", and the fonts are usually not very large, which is both eye-catching and time-consuming. If you want to find some text-to-speech software, there are countless good software. But until I saw the pyttsx3 (explanation below) library, I felt from the bottom of my heart that Python is really powerful. It can realize the text-to-speech function without calling other network interfaces. As a Python learner, how can I lose this good thing? What about opportunities? 


1. What is pyttsx3

pyttsx3 is a text-to-speech library in Python, the package is available for Windows, Mac and Linux. It uses native voice drivers when available and works completely offline.

Official documentation: Using pyttsx3 — pyttsx3 2.6 documentation or pyttsx3 · PyPI

Basic usage ( install the library first ):

import pyttsx3

"""语言播放Hello World"""
engine = pyttsx3.init()
engine.say("Hello World!")
engine.runAndWait()


2. Install pyttsx3

The installation command is as follows:

pip install pyttsx3==2.90

If the download is too slow or the download is too slow, you can use pip install pyttsx3==2.90 -i https://pypi.douban.com/simple/ As for why the download is too slow or the download is too slow, interested friends can refer to the following article: The method to solve the problem of slow pip download speed and timeout error is suitable for a variety of operating systems (details)_import _this's blog-CSDN blog As an excellent Python programmer, you must deal with pip. Those who have used the pip download library Partners must know that its download speed cannot be described as slow. This is mainly because pip uses foreign sources by default, so the download speed will be slow. Of course, this is also related to the mood of some regional networks and pip official mirror source servers. , the most direct solution is to replace it with a domestic download source. https://blog.csdn.net/python_sy/article/details/126710868


3. Check the voice package

pyttsx3 can view all voice packages in the computer, enter the following code to view voice packages.

import pyttsx3

count = 0
engine = pyttsx3.init()
voices = engine.getProperty('voices')

for voice in voices:
    count += 1
    print("语音包%s:" % count)
    print(" - ID: %s" % voice.id)
    print(" - 姓名: %s" % voice.name)
    print(" - 语言: %s" % voice.languages)
    print(" - 性别: %s" % voice.gender)
    print(" - 年龄: %s\n" % voice.age)

print("共有%s种语音包" % count)


4. Add voice package

Generally, the Windows operating system comes with two kinds of voice packs. If you think it is not enough, you can download the voice packs.

1. Download the voice pack

Microsoft voice pack download address:

https://www.microsoft.com/en-us/download/details.aspx?id=27224

You can choose to download Chinese related voice packs

MSSpeech_ SR _zh-CN_TELE.msi
MSSpeech_SR_zh-TW_TELE.msi
MSSpeech_SR_zh-HK_TELE.msi
MSSpeech_ TTS _zh-CN_HuiHui.msi
MSSpeech_TTS_zh-TW_HanHan.msi
MSSpeech_TTS_zh-HK_HunYee.msi

According to the prompt, the operating environment needs to be installed first.
If the software is open, you also need to install the SDK

Terms involved:
TTS (Text To Sound) is text-to-speech
SR (Speech Recognition) is speech recognition

Note: Here we mainly download TTS:

MSSpeech_ TTS _zh-CN_HuiHui.msi (Mandarin)
MSSpeech_TTS_zh-TW_HanHan.msi (Taiwan)
MSSpeech_TTS_zh-HK_HunYee.msi (Hong Kong)

If you find the Microsoft voice package downloading process a bit troublesome, you can directly use the MSSpeech_TTS_zh-TW_HanHan.msi voice package and operating environment that I downloaded (Note: This is a Taiwanese voice package , because the default on my computer is the HuiHui voice package. But MSSpeech_TTS_zh-CN_HuiHui.msi voice package is also in the link )

链接:https://wwp.lanzoub.com/b02vkiefg
提取码:90u1

2. Install voice pack

Note: Take the HanHan voice package posted in my link as a demonstration , and others can be deduced by analogy.

1. Install SpeechPlatformRuntime.msi first (double-click to install), restart the computer after the installation is complete

2. Restart the computer and install MSSpeech_TTS_zh-TW_HanHan.msi (double-click to install)

3. Win key + R key, enter regedit in the pop-up window, and open the path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\Voices\Tokens in the pop-up registry editor to see the new installation. Voice package (HanHan):

4. Right-click HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\Voices , export it to a .reg file, use a text editor or other text editing software to open the .reg file you just saved, and change the \Speech Server\ In v11.0 , replace all files with \Speech and save.

5. Double-click the modified .reg file. (If a warning window pops up, don't worry, just click Yes )

6. Open the registry editor again and go to the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens path to see the newly installed voice package and the computer's default voice package:


5. Function implementation and resource download

1. Download the required libraries

pip install pyttsx3==2.90

pip install pygame==2.1.2
pip install PySimpleGUI==4.60.3

If the download is too slow or the download is too slow, you can use pip install pyttsx3==2.90 -i https://pypi.douban.com/simple/ As for why the download is too slow or the download is too slow, interested friends can refer to the following article ( You don’t need to download pyttsx3 if you have downloaded it):

The method to solve the problem of slow pip download speed and timeout error is suitable for a variety of operating systems (details)_import _this's blog-CSDN blog As an excellent Python programmer, you must deal with pip. Those who have used the pip download library Partners must know that its download speed cannot be described as slow. This is mainly because pip uses foreign sources by default, so the download speed will be slow. Of course, this is also related to the mood of some regional networks and pip official mirror source servers. , the most direct solution is to replace it with a domestic download source. https://blog.csdn.net/python_sy/article/details/126710868

2. Runtime interface

You can switch between Chinese and English (this function is purely installed to make the interface more perfect)

  

3. Resource download

I have packaged the source code and the exe executable file. If you like it, please pay attention or click a little like.

下载链接:https://wwp.lanzoub.com/b02vko1yd
密码:92lg

Guess you like

Origin blog.csdn.net/python_sy/article/details/126809608