新聞中心
python程序有有許多有助于高效編程的模塊和第三方包,了解這些模塊的正確使用方法是很重要的,下面本篇文章就來給大家總結(jié)分享10個有趣且實用的Python模塊,一起看看他們的功能吧!

創(chuàng)新互聯(lián)公司公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元寶安做網(wǎng)站,已為上家服務(wù),為寶安各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
閑話少說,我們直接開始吧。 :)
Faker是一個python軟件包,可以在終端中使用pip install Faker安裝。每次運行以下程序faker generator時,都將產(chǎn)生不同的隨機(jī)數(shù)據(jù)。
from faker import Faker fake = Faker() print(fake.name()) print(fake.email()) print(fake.country()) print(fake.profile())
輸出如下:
2.手寫文本圖像
為了完成上述功能,需要第三方程序包pywhatkit,可以使用pip install pywhatkit進(jìn)行安裝。這個軟件包有很多其他功能,比如在谷歌上搜索等。
樣例代碼如下:
import pywhatkit
pywhatkit.text_to_handwriting('''Learning Python from the basics is extremely important. Before starting to learn python,understanding a base language like c is a must and some of the oops concepts.Python program has many modulesand packages, which helps with efficient programming.
Understanding these modules and 1proper usage of many syntax and libraries is recommended.
In this article, a few modules and packages are used in the program.
Python includes tons of libraries and some of them are quiet intresting''')
輸出如下:
輸出以圖像文件形式保存在當(dāng)前python文件目錄下。
3.實現(xiàn)電腦關(guān)機(jī)
實現(xiàn)上述功能需要用到OS庫,可以使用pip install os進(jìn)行安裝。我們可以使用該庫來實現(xiàn)關(guān)閉,重啟,或者設(shè)置關(guān)閉重啟倒計時等功能。
樣例代碼如下:
import os
shutdown = input("Do you want to shutdown your computer (yes / no): ")
if shutdown == 'yes':
os.system("shutdown /s /t 1")
else:
print('Shutdown is not requested')
注意事項如下:
4.打印日歷
Python中有一個內(nèi)置模塊calendar,它可以幫助訪問日歷。在這個模塊中有很多方法,在下述程序中,我們試圖打印一年中指定月份的日歷。
import calendar
year =int( input("Enter the year of the required calendar "))
month = int( input("Enter the month of the required calendar "))
print(calendar.month(year,month))
運行結(jié)果如下:
5.畫一個餅圖
在下述程序中,我們使用matplotlib來畫餅圖。可以使用pip install matplotlib安裝此庫。有了這個模塊,可以用python編寫許多交互式視覺效果。
樣例代碼如下:
import matplotlib.pyplot as plt
Partition = 'Holidays', 'Eating_Out', 'Shopping', 'Groceries'
sizes = [250, 100, 300, 200]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=Partition, autopct='%1.1f%%', shadow=True, startangle=90)
ax1.axis('equal')
plt.show()
運行結(jié)果如下:
6.彈出告警框
下述程序使用第三方庫pyautogui 來顯示告警框。通常來說,可以使用pip install pyautogui來安裝它。這個模塊有很多方法,比如使用python程序來控制鼠標(biāo)和鍵盤。
樣例代碼如下:
import pyautogui
num=int(input("Enter a value to divide 100"))
if num == 0:
pyautogui.alert(" Alert!!! 100 cannot be divided by 0")
else:
print(f'The value is {100/num}')
輸出如下:
7.文本轉(zhuǎn)語音
為了實現(xiàn)將文本轉(zhuǎn)化為音頻,需要使用pip install pyttsx3 來安裝一個轉(zhuǎn)換庫。這個庫有很多模塊,我們還可以嘗試改變音頻的聲音、音量和速度。
樣例代碼如下:
import pyttsx3
engine = pyttsx3.init()
engine.say('This is a python example in MEDIUM')
engine.runAndWait()
上述代碼運行后,輸出是一個女性聲音,將對應(yīng)的文字轉(zhuǎn)化為音頻播放。
8.截圖
如下代碼所示,我們使用python庫pyautogui 來實現(xiàn)截屏功能。代碼如下:
import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
上述代碼運行后,輸出文件截圖保存在 python 源文件目錄下。我們可以嘗試使用 time.sleep() 語法來延遲屏幕截圖。
9.網(wǎng)絡(luò)監(jiān)測
在下述程序中,為了監(jiān)測互聯(lián)網(wǎng)速度,我們使用了speedtest 庫,要安裝此第三方庫,可以使用 pip install speedtest-cli 語法進(jìn)行安裝。
代碼如下:
import speedtest
speed = speedtest.Speedtest()
download_speed = speed.download()
upload_speed = speed.upload()
print(f'The download speed is {download_speed}')
print(f'The uplaod speed is {upload_speed}')
運行結(jié)果如下:
10.用 Python 繪制圖形
在下述程序中,我們使用 Python中的Turtle 繪制了一個螺旋圖。要安裝該庫,可以使用pip install PythonTurtle。 Python Turtle主要用于繪制視覺圖形,以及圖形的形狀顏色設(shè)置。
樣例代碼如下:
import random
import turtle
colors = ['red','cyan','pink' ,'yellow', 'green','orange']
t = turtle.Turtle()
t.speed(10)
turtle.bgcolor("black")
length=100
angle =50
size=5
for i in range(length):
color=random.choice(colors)
t.pencolor(color)
t.fillcolor(color)
t.penup()
t.forward(i+50)
t.pendown()
t.left(angle)
t.begin_fill()
t.circle(size)
t.end_fill()
turtle.exitonclick()
turtle.bgcolor("black")
運行結(jié)果如下:
總結(jié)
本文重點匯總了使用Python中的第三方庫來實現(xiàn)常見場景下的一些簡單有趣的功能,并給出了樣例程序和相應(yīng)的解釋。
當(dāng)前文章:分享10個有趣且實用的Python模塊,看看他們的功能吧!
當(dāng)前網(wǎng)址:http://m.fisionsoft.com.cn/article/ccosdij.html


咨詢
建站咨詢
