0

I am trying to convert text to speech in Python 3.10.2 using the code:

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")

But there is continuously occuring an error:

Traceback (most recent call last):   File "d:\Program\Python programing\tempCodeRunnerFile.py", line 1, in <module>
    import win32com.client   File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\__init__.py",
line 10, in <module>
    from . import dynamic   File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\dynamic.py",
line 24, in <module>
    from . import build   File "C:\Users\ASUS\AppData\Roaming\Python\Python310\site-packages\win32com\client\build.py",
line 638, in <module>
    valid_identifier_chars = string.ascii_letters + string.digits + "_" AttributeError: module 'string' has no attribute 'ascii_letters'
5
  • Looking at the file name programing\tempCodeRunnerFile.py, it looks like you had some portion of your program selected when you ran the program using "Code Runner" extension. Did you try running it without the selection? Commented Feb 9, 2022 at 8:46
  • import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") speaker.Speak("Hello, it works!") Is it really one line in your actual code? Commented Feb 9, 2022 at 8:57
  • please insert your code and traceback as is without conversions. And use code block ({ } mark) instead of quote Commented Feb 9, 2022 at 9:02
  • Your traceback contains AttributeError: module 'string' has no attribute 'ascii_letters'. This is weird, because the string module of the standard library does have this attribute. Could you have a custom module named string? Commented Feb 9, 2022 at 10:02
  • If you create a new .py file containing just these three lines, does it work? If not, the error is elsewhere in tempCodeRunnerFile.py. Commented Feb 19, 2022 at 11:32

1 Answer 1

1

you wrote two lines of code in the same line Try this

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")

or try to put a semicolon after the first line of code Try this

import win32com.client; speaker =win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")

either way should work

Sign up to request clarification or add additional context in comments.

3 Comments

First of all, thank you for your answer. I tried both ways that you recommend but it throws the same error as earlier.
While I write the code the.Speak() function is not highlighted
I just copied your code and did that small edit and that worked for me. I don't know the solution for sure but if I were you I would try to reinstall python, because it seems that the error is in importing the library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.