1通过 pip install pywin32-227-cp37-cp37m-win_amd64.whl 安装好后打开Pythonwin.exe 报错
这样并没有真正安装成功,只有部分功能正常,比如语音识别,就不能正常,比如下面测试代码,就报None
from win32com.client import constants
import os
import win32com.client
import pythoncom
class SpeechRecognition:
def __init__(self, wordsToAdd):
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
self.context = self.listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
self.wordsRule.Clear()
[self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
self.grammar.Rules.Commit()
self.grammar.CmdSetRuleState("wordsRule", 1)
self.grammar.Rules.Commit()
self.eventHandler = ContextEvents(self.context)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
newResult = win32com.client.Dispatch(Result)
mytext=newResult.PhraseInfo.GetText()#识别的词语
if mytext=="关机":
os.system("shutdown -s -t 300")
elif mytext=="取消关机":
os.system("shutdown -a")
if __name__ == '__main__':
wordsToAdd = ["关机", "取消关机"]
speechReco = SpeechRecognition(wordsToAdd)
while True:
pythoncom.PumpWaitingMessages()
解决办法,直接下载可执行文件安装
下载地址:
https://github.com/mhammond/pywin32/releases/download/b227/pywin32-227.win-amd64-py3.7.exe
安装完成后,如果系统安装了多个python 版本,pycharm 需要调整到指定python解释器