Use Windows 10 speech synthesis with Python

Port the VBScript code created in the following article and command it for easy use.

Please refer to the following article for how to create a video with this command.

Please refer to the following article for how to specify the pronunciation.

Preparation

A list of audio supported by Windows 10.

Add if you want to use a language other than Japanese.

Install pywin32 with pip to use COM.

Library installation


py -m pip install pywin32

Transplant

Get the available audio.

voices2.py


import win32com.client
cat  = win32com.client.Dispatch("SAPI.SpObjectTokenCategory")
cat.SetID(r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices", False)
for token in cat.EnumerateTokens():
    print(token.GetDescription())

Execution result


Microsoft Ayumi - Japanese (Japan)
Microsoft Naayf - Arabic (Saudi)
Microsoft Ivan - Bulgarian (Bulgaria)
Microsoft Herena - Catalan (Catalan)
Microsoft Jakub - Czech (Czech Republic)
(Omitted below)

This is an example of reading aloud by specifying the voice.

sayaka.py


import win32com.client
sapi = win32com.client.Dispatch("SAPI.SpVoice")
cat  = win32com.client.Dispatch("SAPI.SpObjectTokenCategory")
cat.SetID(r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices", False)
v = [t for t in cat.EnumerateTokens() if t.GetAttribute("Name") == "Microsoft Sayaka"]
if v:
    oldv = sapi.Voice
    sapi.Voice = v[0]
    sapi.Speak("Hello World")
    sapi.Voice = oldv

** [Caution] ** </ font> Save the source in UTF-8.

This is an example of outputting audio to the file sayaka.wav.

sayaka-wav.py


import win32com.client
sapi = win32com.client.Dispatch("SAPI.SpVoice")
cat  = win32com.client.Dispatch("SAPI.SpObjectTokenCategory")
cat.SetID(r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices", False)
v = [t for t in cat.EnumerateTokens() if t.GetAttribute("Name") == "Microsoft Sayaka"]
if v:
    fs = win32com.client.Dispatch("SAPI.SpFileStream")
    fs.Open("sayaka.wav", 3)
    sapi.AudioOutputStream = fs
    oldv = sapi.Voice
    sapi.Voice = v[0]
    sapi.Speak("Hello World")
    sapi.Voice = oldv
    fs.Close()

Once you can use COM, the rest is normal Python.

command

Commanded to make it easier to read and save. You can also use it as a library if you refer to it from others.

Example of use


py wintts.py -l
py wintts.py -l ja en
py wintts.py Hello, world
py wintts.py -v sayaka -r 5 Hello, world
py wintts.py -v sayaka -o sayaka.wav -i hello.txt
py wintts.py -v zira -p "h eh - l ow 1"
py wintts.py -v zira -s ipa "hɛ.ˈloʊ"

WSL

You cannot call COM from Python in WSL, but you can call Python on the Windows side from WSL.

Place wintts.py somewhere visible to Windows. Write a simple wrapper like the following and place it wherever the path is in WSL and add the execute attribute.

wintts


#!/bin/sh
py.exe 'C:\Script storage\wintts.py' "$@"

You can now use it as if it were a WSL command.

Example of use


$ py wintts.py -l de fr
de-AT, German (Austria): Microsoft Michael
de-CH, German (Switzerland): Microsoft Karsten
de-DE, German (Germany): Microsoft Hedda
de-DE, German (Germany): Microsoft Katja
de-DE, German (Germany): Microsoft Stefan
fr-CA, French (Canada): Microsoft Caroline
fr-CA, French (Canada): Microsoft Claude
fr-CA, French (Canada): Microsoft Nathalie(Canada)
fr-CH, French (Switzerland): Microsoft Guillaume
fr-FR, French (France): Microsoft Hortense
fr-FR, French (France): Microsoft Julie
fr-FR, French (France): Microsoft Paul
$ wintts -v julie bonjour
$ wintts -o de.wav -v hedda guten tag
$ winplay de.wav

The last call to winplay is the self-made script created in the following article.

Related Links

After writing this article, I saw some articles dealing with SAPI in Python, so I will add it.

Recommended Posts

Use Windows 10 speech synthesis with Python
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Python starting with Windows 7
Use Python 3.8 with Anaconda
Use python with docker
Use Python / Django with Windows Azure Cloud Service!
Python with VS Code (Windows 10)
Use Trello API with python
Run python with PyCharm (Windows)
Use Twitter API with Python
Use TUN / TAP with Python
Use Windows 10 fonts with WSL
Use subsonic API with python3
Use Python on Windows (PyCharm)
Time synchronization (Windows) with Python
Face detection with YOLO Face (Windows10, Python3.6)
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Build mlpy with python3.3 (64bit) (windows 64bit)
Use Python in pyenv with NeoVim
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
Japanese speech synthesis starting with Tacotron2
How to use FTP with Python
Use OpenCV with Python 3 in Window
Getting started with Python 3.8 on Windows
Use PostgreSQL with Lambda (Python + psycopg2)
[Python] Creating multiple windows with Tkinter
[Python Windows] pip install with Python version
End-to-End Text Speech Synthesis Starting with ESPnet2
English speech recognition with python [speech to text]
Run servo with Python on ESP32 (Windows)
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Use Amazon Simple Notification Service with Python
How to use Cmder with PyCharm (Windows)
Building a Python 3.6 environment with Windows + PowerShell
[Python] Use string data with scikit-learn SVM
Easy introduction of speech recognition with Python
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Develop Windows apps with Python 3 + Tkinter (Preparation)
Use without installing python 2.x on Windows
Use Application Insights with Python 3 (including bottles)
Use C ++ functions from python with pybind11
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
FizzBuzz with Python3
Scraping with Python
Statistics with python
Python on Windows
Scraping with Python
Use Jupyter Notebook with Visual Studio Code on Windows 10 + Python + Poetry + pyenv-win
python windows environment
Twilio with Python
Integrate with Python