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.
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
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.
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.
After writing this article, I saw some articles dealing with SAPI in Python, so I will add it.
Recommended Posts