Fun and useful libraries for Python

Preface

Let’s take it easy today and introduce you to some interesting and useful libraries in Python! It’s all good stuff!

Generate QR code

You can make the information you want to convey into a QR code and send it there. As for the usage, you can explore it yourself, hahaha!
If you put the QR code, it will become an illegal picture, so don’t put it. A QR code picture will be generated on the same layer of the execution file. Check it yourself.

import pyqrcode  
import png  
from pyqrcode import QRCode  
# 生成二维码
# pip install pyqrcode
# pip install pypng
inpStr = "www.baidu.com"  
qrc = pyqrcode.create(inpStr)  
qrc.png("baidu.png", scale=6) 

strange pictures

This is just for fun, and strange knowledge has been added

import cowsay
# 画图
# pip install cowsay
# print(cowsay.trex('鸡'))
print(cowsay.cow('美'))
 
# 除此之外还有很多
# beavis, cheese, daemon, cow, dragon, ghostbusters, kitty, meow, milk, pig,
# stegosaurus, stimpy, trex, turkey, turtle, tux。

Insert image description here

Comes with mini games

Some small games to relieve boredom

import os
# 游戏
# pip install freegames
# 查看所有游戏名称
os.system('python -m freegames list')
# 运行指定游戏
os.system('python -m freegames.ant')

Insert image description here
Insert image description here

text to speech

This is still a bit of a black technology, allowing you to directly process text into speech, and when doing some script tasks or triggering reminders, you can call it all at once, and you will feel any sudden change.

import pyttsx3
# 文字转语音
# pip install pyttsx3
engine = pyttsx3.init()
engine.say('你咋这么帅!')
engine.runAndWait()  

Download progress bar

Can make some tasks more beautiful

import time
# 下载进度条
total = 132 # 可以用os获取文件大小
for i in range(1, 101):
    time.sleep(0.3)
    print(f'\r共{
      
      total}MB,已下载{
      
      i}MB,{
      
      int(i / total * 100)}%。', end='')

Insert image description here

calendar display

You can display the monthly calendar according to your choice, which is somewhat useful but not very useful.

import calendar
# 日历
year =int(input("请输入年份:"))
month = int(input("请输入月份:"))
print(calendar.month(year,month))

Insert image description here

speed test

This is very useful. It saves me from looking for websites every day, which are unreliable. This B-square is up in no time, especially when I use it to repair the computer of the goddess, the effect is better.

import speedtest  
# 测网速
# pip install speedtest-cli
 
print("准备测试ing...")
 
# 创建实例对象
test = speedtest.Speedtest()
# 获取可用于测试的服务器列表
test.get_servers()
# 筛选出最佳服务器
best = test.get_best_server()
 
print("正在测试ing...")
 
# 下载速度 
download_speed = int(test.download() / 1024 / 1024)
# 上传速度
upload_speed = int(test.upload() / 1024 / 1024)
 
# 输出结果
print("下载速度:" + str(download_speed) + " Mbits")
print("上传速度:" + str(upload_speed) + " Mbits")

Insert image description here

word cloud generation

I don’t know what it does, but it seems very high-end
and has many uses. git is there, so go explore it yourself.

import stylecloud
# 词云 https://github.com/minimaxir/stylecloud
# pip install stylecloud
stylecloud.gen_stylecloud(file_path='./data/text.txt',icon_name="fas fa-dragon",output_name='dragon.png')

Randomly generate dummy data

If this thing works well, then creating data will become child's play.

import random
 
from openpyxl import workbook
from faker import Faker
# 生成随机数据
# pip install faker
wb = workbook.Workbook()
sheet = wb.worksheets[0]
sheet.title = 'pd练习'
 
li = ['序号', '姓名', '年龄', '性别', '健康程度', '国家']
di = {
    
    '中国': 'zh_CN', '美国': 'en_US', '法国': 'fr_FR', '日本': 'ja_JP'}
 
with open('new_message.xlsx', mode='w', encoding='utf-8') as f:
    for num, item in enumerate(li, 1):
        sheet.cell(1, num).value = item
 
    for num, i in enumerate(range(2, 502), 1):
        country = random.choice(['中国', '美国', '法国', '日本'])
        gender = random.choice(['男', '女'])
 
        fk = Faker(locale=di[country])
        sheet.cell(i, 1).value = num
        sheet.cell(i, 2).value = fk.name_male() if gender == '男' else fk.name_female()
        sheet.cell(i, 3).value = random.randint(14, 66)
        sheet.cell(i, 4).value = gender
        sheet.cell(i, 5).value = round(random.random(), 2)
        sheet.cell(i, 6).value = country
 
wb.save('new_message.xlsx')

Insert image description here

Draw a pie chart

Make your data display more vivid

import matplotlib.pyplot as plt
# 绘制饼图
Partition = 'Teams A','Teams B','Teams C','Teams D'
sizes = [250,100,150,200]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=Partition, autopct='%1.1f%%',shadow=True, startangle=90)
ax1.axis('equal')
plt.show()

Insert image description here

get your ip address

This is also an operation that makes you look high-end but seems to be unnecessary.

import socket as f  
# 获取你的ip地址
hostn = f.gethostname()  
Laptop = f.gethostbyname(hostn)  
print("你的电脑本地IP地址是:" + Laptop) 

Insert image description here

Summarize

The above are some interesting Python libraries introduced today. Of course, there are many other interesting libraries. Let’s explore them together!

Guess you like

Origin blog.csdn.net/VincentLee7/article/details/126984383