# 实用工具

python-office 提供了一系列实用小工具,目前有:

安装:pip install python-office

# 1、生成二维码

视频说明:传送门

将任意文本或链接生成为二维码图片。

import office

office.tools.qrcode(
    content='https://www.python-office.com',  # 二维码内容(文本或链接)
    output_path='qrcode.png'                   # 输出图片路径
)

参数说明:

参数 类型 必填 说明
content str 二维码内容,可以是文字或 URL
output_path str 输出图片路径,默认保存在当前目录

# 2、翻译

视频说明:传送门

一行代码实现中英文互译。

import office

result = office.tools.translate(
    content='Hello, World!',  # 要翻译的内容
    to='zh'                    # 目标语言,zh:中文,en:英文
)
print(result)

参数说明:

参数 类型 必填 说明
content str 要翻译的文本
to str 目标语言,zh(中文)或 en(英文),默认中文

# 3、密码生成器

文字说明:源码

自动生成随机强密码,避免手动设置简单密码带来的安全隐患。

import office

password = office.tools.create_password(length=16)  # 生成16位随机密码
print(password)

参数说明:

参数 类型 必填 说明
length int 密码长度,默认 8 位

# 4、查询天气

文字说明:传送门

查询指定城市的实时天气信息。

import office

office.tools.weather(city='北京')

参数说明:

参数 类型 必填 说明
city str 城市名称

# 5、汉字转拼音

视频说明:传送门

将汉字转换为拼音,支持批量处理。

import office

result = office.tools.han2pinyin(content='程序员晚枫')
print(result)  # cheng xu yuan wan feng

参数说明:

参数 类型 必填 说明
content str 要转换的汉字内容

# 相关课程

Last Updated: 4/6/2026, 10:18:15 AM