💻 大家好,我是哈桑c。本文为大家介绍 Python 中有趣的第三方库,希望大家看完有所收获。
关于本文中介绍的所有的第三方库,笔者并不会每一个都进行详细的介绍,因为如果都一一进行介绍的话整篇文章就显得太啰嗦了。所有更多还是针对这些库的要点进行讲解,关于更多的用法大家可以到文中给出的官方或 github 网址进行更多的了解,本文只起到一个介绍和引导作用。
以 Windows10 为例,可以按住 win + R 键输入 cmd 打开命令行窗口,输入 pip install xxx 命令下载我们需要的 Python 第三方库。
示例如下:
如果部分库下载不成功或者下载速度太慢的话,可以使用镜像源下载,以阿里源为示例:
pip install xxx -i https://mirrors.aliyun.com/pypi/simple
没有报错的话就是安装成功了。
phone 是 Python 的一个第三方库,开发者可以通过输入手机号码来获取该号码的归属地、区号以及运营商等信息,使用方法十分简单。
点击跳转 github 网站
代码示例:
from phone import Phone#实例化phone对象
phone_obj = Phone() # 使用find方法解析电话号码
res = phone_obj.find("13366668888")print(res)
运行结果:
geoip2 库为广泛的应用程序提供了 IP 地理定位和代理检测功能。开发者可以在 Python 中下载好 geoip2 第三方库,直接导入即可使用。
点击跳转官方网站
代码示例:
from geoip2.database import Reader# 导入GeoLite2-City.mmdb文件
reader = Reader("./GeoLite2-City.mmdb")# 使用city方法解析百度知道的ip地址(公开),获取result响应对象
my_ip = "202.108.22.103"
result = reader.city(my_ip)print(result)
运行结果:
注意程序中的 GeoLite2-City.mmdb 文件可以到官网上去下载最新版。当然如果不要求是最新版的文件的话,也可以像笔者一样直接去 github 上下载一个。
freegames 库是一个 Apache2 授权的免费 Python 游戏集,旨在教育和娱乐。这些游戏是用简单的 Python 代码编写的,旨在进行实验和更改。几个经典街机游戏的简化版本包括在内。
点击跳转 github 网站
代码示例:
# freegames库安装成功之后,可以使用os库运行终端命令。
import os # 查看所有游戏名称
os.system("python -m freegames list")# 运行指定游戏
os.system("python -m freegames.connect")
运行结果:
JioNLP 是一个面向 NLP(自然语言处理)开发者的工具包,提供 NLP 任务预处理、解析功能,准确、高效、零使用门槛。当我们拿到完整的地址信息,但想要从地址信息中解析出指定的单元信息时,就可以使用 jionlp 库。
点击跳转 github 网站
代码示例:
import jionlpmy_address = "北京市东城区景山前街4号"# 使用 jionlp.parse_location 解析地址
result = jionlp.parse_location(my_address, town_village=True)print(result)
运行结果:
qrcode 是 Python 的一个用于生成 QRCode (快速响应码,也是二维码的一种)的开源第三方库。使用该库时,可以使用快捷的 make 方法生成指定文本内容的二维码。
点击跳转 github 网站
代码示例:
import qrcode# 指定扫描二维码后显示的信息
data = '鸡你太美'# 使用make方法创建二维码
img = qrcode.make(data)# 显示图片格式,输出
print(type(img))# 保存图片
img.save("test1.png")
运行结果:(test1.png 文件)
二维码放不上来,违规了。
python-barcode 提供了一种在 Python 中创建条形码的简单方法。生成 SVG 文件时也不需要外部依赖关系,同时还支持生成多种格式的条形码。
点击跳转 github 网站
代码示例:
import barcode
from barcode.writer import ImageWriterprint(barcode.PROVIDED_BARCODES) #查看 python-barcode支持的条形码格式EAN = barcode.get_barcode_class('ean13') #创建ean13格式的条形码格式对象ean = EAN('5901234123662',writer=ImageWriter()) #创建条形码对象,内容为5901234123457fullname = ean.save('./test') #保存条形码图片,并返回保存路径。print(fullname)
运行结果:(test.png 文件)
PyWhatKit 是一个具有各种有用功能的 Python 库。其中包括发送 WhatApp 消息、播放 YouTube 视频、执行谷歌搜索,索取指定主题信息以及将文本转换为手写文本图像等,本文演示的是生成手写体的功能。
点击跳转 github 网站
代码示例:
import pywhatkitmy_text = """A child was careless ramie stabbed, he rushed home and told his mother: I only lightly Pengyi what, it was my painful thorns.Mom said: Because of this, it will thorn you. if the next time you met Ramie, to a courageous and seize it,it will be in your hands become soft as silk, you will no longer be stabbed.It is said that many people are serving hard against soft."""pywhatkit.text_to_handwriting(my_text, "test2.png", (30, 30, 150))
运行结果:(test2.png 文件)
pyttsx3 是 Python 中的文本到语音转换库。与其他库不同,pyttsx3 库支持脱机工作。
点击跳转 github 网站
代码示例:
import pyttsx3engine = pyttsx3.init() # 初始化引擎
engine.say('Hello,world') # 指定需要读取的文本
engine.runAndWait()
运行上例代码,电脑就会读出 Hello,world,同时 pyttsx3 库也支持读取中文。
PyAutoGUI 允许 Python 脚本控制鼠标和键盘,以自动化与其他应用程序的交互。API 的设计非常简单。PyAutoGUI 适用于 Windows 、macOS 和Linux ,并运行在 Python2 和3上。
点击跳转官方网站
代码示例:
import pyautogui#获取主显示器的大小。
screenWidth, screenHeight = pyautogui.size() print(f"显示器的宽度为:{screenWidth}")
print(f"显示器的高度为:{screenHeight}")
运行结果:
cowsay 是一个生成 ASCII 图片的程序,可以显示一只奶牛的界面。它也可以使用预先制作的图像,以生成其他动物的图片,如 Linux 的吉祥物企鹅。由于它是用 Perl 语言编写的,它也适用于其他系统,如微软的 Windows 。
点击跳转 github 网站
代码示例:
from cowpy import cow# 创造一头奶牛
cheese = cow.Moose()# 通过挤牛奶来获取cowsay信息
msg = cheese.milk("My witty mesage")# 打印输出msg
print(msg)
运行结果:
stylecloud 是一款可以制作个性化词云图的 Python 库。它利用了流行的 word_cloud 包,添加了有用的功能来创建真正独特的单词云! 是一个操作简单,可直接调用的 Python 库。
点击跳转 github 网站
代码示例:
import stylecloudstylecloud.gen_stylecloud(file_path='./replay.txt',icon_name="fas fa-apple-alt")
replay.txt 文件:
Twilight west, leaving a touch of sadness. Half curtain in the
sunset, drunk dizzy stars, the moon hidden mountain. A willow
falling back of the shadow of the lonely slide into the mountain
bend, quietly sleep. In the haze of smoke, the years pass in a
hurry, chasing a round of dreams far away. Half a wall, two blocks
of the city, the firefly fragrance of night.Cut a period of years,
write a past, time in the wind and moon lingering, light as Zen.
运行结果:
psutil 是 Python 的一个跨平台的系统信息库,用于在 Python 中检索正在运行的进程和系统利用率( CPU、内存、磁盘、网络、传感器)的信息。它主要用于系统监控、分析和限制进程资源以及管理正在运行的进程。
点击跳转 github 网站
代码示例:
import psutil# 获取CPU的核心数
cpu_count = psutil.cpu_count(logical=True)# 获取CPU使用频率
cpu_frequency = psutil.cpu_percent(interval=1, percpu=True)print(f"CPU逻辑核心数为: {cpu_count}")
print(f"CPU使用率为: {cpu_frequency}")
运行结果:
Pdf2docx 是一个 PDF 转 Word 的 Python 库。是使用 PyMuPDF 从 PDF 中提取数据,例如文本,图像和 drawingsParse 布局规则以及节,段落,图像和表格,然后使用 python-docx 生成 docx 。
点击跳转 github 网站
代码示例:
from pdf2docx import Converterpdf_file = './Example.pdf'
docx_file = './Example.docx'# 转换为 docx 格式文件
cv = Converter(pdf_file)
cv.convert(docx_file, start=0, end=None)
cv.close()
运行结果:
成功转换为 word 文件了。
mimesis 是一款 Python 的高性能伪数据生成器库,它以各种语言为各种目的提供数据。假数据可以用来填充测试数据库,创建假 API 端点,创建任意结构的 JSON 和 XML 文件,匿名化生产数据等等。
点击跳转 github 网站
代码示例:
from mimesis import Person
from mimesis.locales import Locale# 指定语言
person = Person(Locale.EN)# 生成一个完成的名字
full_name = person.full_name() # 生成指定格式电子邮箱
my_email = person.email(domains=["example.com"])print(full_name)
print(my_email)
运行结果:
moderni 是一个 Python 第三方库,用于读取 Python2 源代码,并应用一系列修复器将其转换为在 Python 3 和 Python 2.7 上都有效的源代码。
点击跳转 github 网站
代码示例:
# Python2的代码(test2.py文件)
print '3 / 2 =', 3 / 2
import os # 执行终端命令
os.system("python -m modernize -w test2.py")
运行结果:(test2.py 文件的 python2 代码已经转换为 python3 代码)
Rembg 是一个删除图像背景的工具的 Python库。有了 rembg 库再搭配 Python 程序,我们就可以做到批量的抠图。
点击跳转 github 网站
代码示例:
import os os.system("rembg i ./input.jpg ./output.png")
原文件:
运行结果:
captcha 是 Python 的一个生成 Laravel 验证码的库。只需要指定宽度、高度、字体和字体大小的参数就可以生成指定的验证码图片。
点击跳转 github 网站
代码示例:
# 导包
# 导包
from captcha.image import ImageCaptcha
from PIL import Image
import random
import string# 定义图片对象
image = ImageCaptcha()# 获取字符串
captcha_text = "".join(random.sample(string.ascii_letters, 4))# 生成图像
captcha_image = Image.open(image.generate(captcha_text))# 存储图片
captcha_image.save('./ver_code.png')
运行结果:
💾 以上就是关于 Python 一些简单实用的第三方库的介绍啦。希望对大家有所帮助。感谢大家的支持。