rich终端富文本 dotenv使用介绍 生成二维码 读取二维码

2025-11-25 22:28:00
admin
原创 701
摘要:rich终端富文本 dotenv使用介绍 生成二维码 读取二维码

一、rich终端富文本

1、代码仓库:https://github.com/Textualize/rich

2、帮助文档:https://rich.readthedocs.io/en/latest

3、样式定义:https://rich.readthedocs.io/en/latest/style.html

4、样式标记:https://rich.readthedocs.io/en/latest/markup.html

5、颜色名称:https://rich.readthedocs.io/en/latest/appendix/colors.html

6、打印JSON:https://rich.readthedocs.io/en/latest/console.html#printing-json

7、rich安装:pip install rich


优雅输出:

from rich import print

from rich.pretty import pprint


优雅输出(交互式场景):

from rich import pretty
pretty.install()


优雅输出(复杂样式):

from rich.console import Console
console = Console()
console.print("Hello", "World!", style="bold red")


优雅输出JSON:

from rich import print_json
print_json(data=config)


优雅侦查:

from rich import inspect
my_list = ["foo", "bar"]
inspect(my_list, methods=True)


二、dotenv使用介绍

1、代码仓库:https://github.com/theskumar/python-dotenv

2、帮助文档:https://saurabh-kumar.com/python-dotenv

3、加载文件:脚本所在目录开始查找.env,查找不到则查找父目录,直到查找到文件;

4、覆盖变量:默认不覆盖环境变量override=False,除非显示设置override=True;

5、dotenv安装:pip install python-dotenv


三、pyqrcode生成二维码

1、pyqrcode代码仓库:https://github.com/mnooner256/pyqrcode

2、pyqrcode安装:pip.exe install pyqrcode,pip.exe install pypng

3、scale每个方块多少像素,quiet_zone边缘有几个空白方块;

4、容错率:L=7%、M=15%、Q=25%、H=30%,容错率越高,空间利用率越低;

5、编码方式:numeric、alphanumeric、binary、kanji,单个字符所用比特递增;


代码示例:

import pyqrcode
qr = pyqrcode.create('Some data', error='M', mode='binary')
qr.png('some_file0.png', scale=10)
qr.show(scale=10,quiet_zone=4)
print(qr.terminal())


四、qrcode生成二维码(推荐使用)

1、qrcode代码仓库:https://github.com/lincolnloop/python-qrcode

2、qrcode安装:pip.exe install "qrcode[pil]"

3、box_size每个方块多少像素,border边缘有几个空白方块;


代码示例:

import qrcode
img = qrcode.make('Some data')
img.save('some_file1.png')


配置参数:

import qrcode

qr = qrcode.QRCode(version=1,box_size=10,border=4,
    error_correction=qrcode.constants.ERROR_CORRECT_M)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(fill_color='black', back_color='white')
img.save('some_file2.png')


五、zxing-cpp读取二维码

1、代码仓库:https://github.com/zxing-cpp/zxing-cpp

2、帮助文档:https://zxing-cpp.github.io/zxing-cpp/docs

3、python包装器介绍:https://pypi.org/project/zxing-cpp

4、python包装器接口:https://github.com/zxing-cpp/zxing-cpp/blob/master/wrappers/python/zxing.cpp

5、安装包装器:pip install zxing-cpp

发表评论
评论通过审核之后才会显示。