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

github star

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

每天忙得团团转,却不知道时间都去哪儿了?

今天教你怎么用 python-office 的时间工具,自动记录和统计时间。

1、记录时间戳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import office
from datetime import datetime

# 记录开始时间
office.time.record_start('写报告')

# ... 做你的工作 ...

# 记录结束时间
office.time.record_end('写报告')

# 查看记录
records = office.time.get_records()
print(records)

2、生成时间报告

1
2
3
4
5
6
7
8
9
10
import office

# 生成本周时间报告
report = office.time.report(
start_date='2024-01-01',
end_date='2024-01-07',
format='excel' # excel/json/html
)

print(report)

运行后,会生成一份详细的时间统计报告。

3、番茄钟功能

专注工作25分钟,休息5分钟:

1
2
3
4
5
6
7
8
import office

# 启动番茄钟
office.time.pomodoro(
work_minutes=25, # 工作时间
break_minutes=5, # 休息时间
rounds=4 # 轮数
)

命令行会显示倒计时,到点提醒你休息。

4、定时提醒

1
2
3
4
5
6
7
8
9
import office

# 设置提醒
office.time.remind(
message='该喝水了!',
minutes=30 # 30分钟后提醒
)

print('提醒已设置,30分钟后提醒你~')

5、实战案例:自动记录每日工作

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
import office
from datetime import date

def daily_time_tracker():
"""每日时间追踪"""
today = date.today().strftime('%Y-%m-%d')

# 开始记录
print(f'开始记录 {today} 的时间...')

tasks = ['写代码', '开会', '写文档', '学习', '回复邮件']

for task in tasks:
input(f'按回车开始 {task}...')
office.time.record_start(task)

input(f'{task} 完成了,按回车...')
office.time.record_end(task)

# 生成报告
report_file = f'时间报告_{today}.xlsx'
office.time.report(start_date=today, end_date=today, format='excel', output=report_file)

print(f'今日时间报告已生成: {report_file}')

daily_time_tracker()

6、实战案例:统计本周时间分布

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import office

# 统计本周时间
report = office.time.report(
start_date='2024-01-01',
end_date='2024-01-07',
format='json'
)

# 分析时间分布
total_hours = sum([r['hours'] for r in report.values()])

for task, hours in report.items():
percentage = (hours['hours'] / total_hours) * 100
bar = '█' * int(percentage / 5)
print(f'{task}: {bar} {percentage:.1f}%')

7、常见问题

Q:记录丢失了怎么办?

A:默认保存在本地文件,可以定期备份。

Q:能导出到日历吗?

A:可以用 format='ics' 导出 iCalendar 格式,导入到日历APP。

Q:番茄钟声音太小?

A:可以配合系统通知,声音更明显。

8、下讲预告

学会了时间管理,下一讲我们学 进度条工具——让程序运行过程更直观。

敬请期待!


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

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

🎓 AI 编程实战课程

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