👉 项目官网:https://www.python-office.com/ 👈

github star

大家好,这里是程序员晚枫,正在all in AI编程实战

学了这么多功能,今天我们来做一个综合实战——开发一个自己的自动化办公助手。

1、需求分析

我们做一个"小助手"程序,功能包括:

  • 📊 一键处理Excel(读取、写入、合并)
  • 📄 批量处理Word文档
  • 📧 自动发送邮件
  • 📁 文件整理
  • 🔍 快速搜索文件

所有功能做成菜单式操作,小白也能用。

2、创建主程序框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
# 程序员晚枫的自动化办公助手

import office
import os

def show_menu():
"""显示菜单"""
print('''
╔══════════════════════════════════════╗
║ 🐍 程序员晚枫的自动化办公助手 🐍 ║
╠══════════════════════════════════════╣
║ 1. 读取Excel文件 ║
║ 2. 写入Excel文件 ║
║ 3. 合并Excel文件 ║
║ 4. 读取Word文档 ║
║ 5. 发送邮件 ║
║ 6. 整理文件夹 ║
║ 7. 搜索文件 ║
║ 0. 退出程序 ║
╚══════════════════════════════════════╝
''')

def main():
"""主程序"""
while True:
show_menu()
choice = input('请选择功能(输入数字):')

if choice == '1':
path = input('请输入Excel文件路径:')
df = office.excel.read(path=path)
print(df)

elif choice == '2':
print('写入Excel功能开发中...')

elif choice == '3':
folder = input('请输入文件夹路径:')
office.excel.merge(folder_path=folder, output_file='合并结果.xlsx')
print('合并完成!')

elif choice == '4':
path = input('请输入Word文件路径:')
text = office.word.read(path=path)
print(text)

elif choice == '5':
email = input('收件人邮箱:')
title = input('邮件标题:')
content = input('邮件内容:')
office.email.send(email=email, title=title, content=content)
print('发送成功!')

elif choice == '6':
folder = input('请输入文件夹路径:')
office.file.classify(folder=folder, type_rule=True)
print('整理完成!')

elif choice == '7':
keyword = input('请输入搜索关键词:')
folder = input('请输入搜索范围(文件夹路径):')
results = office.file.search(folder=folder, keyword=keyword)
for r in results:
print(r)

elif choice == '0':
print('感谢使用!再见~')
break

else:
print('输入错误,请重新选择')

input('\n按回车继续...')

if __name__ == '__main__':
main()

3、运行程序

1
python office_assistant.py

运行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    ╔══════════════════════════════════════╗
║ 🐍 程序员晚枫的自动化办公助手 🐍 ║
╠══════════════════════════════════════╣
║ 1. 读取Excel文件 ║
║ 2. 写入Excel文件 ║
...
╚══════════════════════════════════════╝
请选择功能(输入数字):1
请输入Excel文件路径:成绩.xlsx
姓名 语文 数学 英语
0 小明 90 85 92
1 小红 88 92 89
2 小刚 76 80 85

按回车继续...

4、增加更多功能

可以在菜单里继续添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 第8个功能:OCR识别
elif choice == '8':
path = input('请输入图片路径:')
text = office.ocr.text(path=path)
print(text)

# 第9个功能:PDF转Word
elif choice == '9':
path = input('请输入PDF文件路径:')
office.pdf.pdf2docx(path=path, output_file='转换结果.docx')
print('转换完成!')

# 第10个功能:图片加水印
elif choice == '10':
path = input('请输入图片路径:')
office.image.add_text(path=path, output_file='水印图.jpg', text='程序员晚枫')
print('水印添加完成!')

5、打包成exe程序

想让别人也能用?打包成 Windows 可执行文件:

1
2
pip install pyinstaller
pyinstaller --onefile --console office_assistant.py

打包后,dist/office_assistant.exe 就是可以单独运行的小工具了。

6、总结

今天我们做了一个自动化办公助手,把之前学的功能整合在一起。

核心代码其实只有几行:

  • office.excel 处理 Excel
  • office.word 处理 Word
  • office.email 处理邮件
  • office.file 处理文件

你可以继续扩展,做出更强大的工具!


有问题欢迎加微信 python-office 进群交流~

程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!