Python Pyttsx3模块

大家有没有让电脑“说话”的欲望,如果我说可以帮大家实现这个愿望的话,大家肯定会说我又要用print函数,但是今天我们就可以真的让电脑说话。

让电脑说话其实很简单,使用python第三方库pyttsx3模块就行了。使用之前还需要安装pyttsx3模块,安装方法:

python终端或cmd输入:

pip install pyttsx3

然后就是导入pyttsx3模块:

import pyttsx3

后面就是使用这个模块,这个模块我们要用到的函数只有三个,分别是init函数、say函数和runAndWait函数。

1.init函数

首先我们需要创建一个对象,把init函数的返回值复制给这个对象,这样这个对象就可以使用pyttsx3模块里的各种函数了。

import pyttsx3

talker = pyttsx3.init()

2.say函数

然后我们就需要让电脑发出声音了。注意say函数里面直接填字符串。

import pyttsx3

talker = pyttsx3.init()

talker.say('Python——CSDN博客')

3.runAndWait函数

最后就是要运行了。

import pyttsx3

talker = pyttsx3.init()

talker.say('Python——CSDN博客')

talker.runAndWait()

4.彩蛋

让pyttsx3朗读一篇英语文章。

代码:

import pyttsx3

talker = pyttsx3.init()

talker.say('The Importance of Lifelong Learning In today\'s rapidly evolving world, the concept of lifelong learning has become increasingly significant. No longer is education confined to the four walls of a classroom or restricted to a specific period in one\'s life. Rather, it is a continuous process that spans from childhood through adulthood and beyond, enriching our lives and expanding our horizons.The first reason for embracing lifelong learning is the pace of change in our society. Technology, in particular, is advancing at an unprecedented speed, bringing about new innovations and disruptions almost daily. To keep up with these changes and stay relevant in our careers and personal lives, we must constantly seek new knowledge and skills. By engaging in continuous learning, we can adapt to the evolving landscape and seize new opportunities.Moreover, lifelong learning fosters personal growth and self-improvement. It allows us to explore new interests, develop our talents, and challenge ourselves in ways we never thought possible. This pursuit of knowledge and self-improvement not only enhances our self-esteem but also enriches our relationships and interactions with others. As we learn more about the world and ourselves, we become more empathetic, understanding, and capable of contributing positively to society.In addition, lifelong learning is crucial for career advancement. In most industries, staying stagnant is equivalent to regressing. Employers value employees who demonstrate a commitment to continuous learning and are willing to invest in themselves through education and training. By acquiring new skills and knowledge, we can increase our marketability, earn promotions, and achieve greater career satisfaction.Furthermore, lifelong learning helps us stay mentally sharp and combat cognitive decline. As we age, our brains undergo changes that can affect memory, learning, and other cognitive functions. Engaging in regular learning activities can stimulate our brains, keep our minds active, and delay the onset of age-related cognitive decline. It also provides a sense of purpose and fulfillment, which is vital for emotional well-being.In conclusion, lifelong learning is a vital aspect of modern life. It enables us to keep up with the pace of change, fosters personal growth, aids career advancement, and promotes mental health. By embracing this concept and making a commitment to continuous learning, we can lead richer, more fulfilling lives and make a positive impact on the world.')

talker.runAndWait()

大家再见!

你可能感兴趣的:(Python,python,开发语言)