自动整理时间记录的代码

作者: 心际花园 | 来源:发表于2024-10-15 09:56 被阅读0次

用于每天记录时间和事件,用程序自动统计,用透视表自动分析。

1.记录时间

记录的格式为
“小时:分钟——小时:分钟,时间”
举例

22:48——6:58 夜睡。
8:09——8:19 听英语。

记录软件是flomo。

2.统计时间

这个代码是对记录进行统计,输出结果为:开始时间、结束时间、时长、事件。
输入是直接复制上面的时间记录,然后运行程序。我用的是PyCharm
Community Edition 2024.1.4。
输出形式是excel表格的数据。

#!python
# 用于整理每天的时间记录。

import pyperclip
import pandas as pd
from datetime import timedelta
import os

text = pyperclip.paste()

def statistical_time_records(text):
    # Split the text into lines
    lines = text.split('\n\n')
    print(lines)

    # Prepare lists to hold the data
    start_times = []
    end_times = []
    events = []

    # Process each line
    for line in lines:
        # Split the line into time part and event part using the last occurrence of ','
        if ' ' in line:
            time_part, event_part = line.rsplit(' ')
        else:
            continue  # Skip lines that don't contain an event part

        # Split the time part into start and end times
        start_time, end_time = time_part.split('——')

        # Clean up the strings
        start_time = start_time.strip()
        end_time = end_time.strip()
        event = event_part.strip()

        # Append to lists
        start_times.append(start_time)
        end_times.append(end_time)
        events.append(event)

    # Create a DataFrame
    df = pd.DataFrame({
        'Start Time': start_times,
        'End Time': end_times,
        'Event': events
    })

    # Convert Start Time and End Time to timedelta
    df['Start Time'] = pd.to_timedelta(df['Start Time'].apply(lambda x: f"00:{x}"))
    df['End Time'] = pd.to_timedelta(df['End Time'].apply(lambda x: f"00:{x}"))

    # Calculate Duration
    def calculate_duration(row):
        if row['End Time'] < row['Start Time']:
            # Calculate the duration using 24:00
            duration = (timedelta(minutes=24) - row['Start Time']) + row['End Time']
        else:
            duration = row['End Time'] - row['Start Time']
        return duration

    df['Duration'] = df.apply(calculate_duration, axis=1)

    # Format Start Time, End Time, and Duration to minute:second
    df['Start Time'] = df['Start Time'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}",
                                                            axis=1)
    df['End Time'] = df['End Time'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}", axis=1)
    df['Duration'] = df['Duration'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}", axis=1)

    df = df[['Start Time', 'End Time', 'Duration', 'Event']]


    # Print the string representation of the DataFrame
    df.to_excel('remote_put.xlsx', index=False)
    # Open the Excel file

    os.system('start excel remote_put.xlsx')

    return df


statistical_time_records(text)

3.分析时间

将数据放到excell里的时间记录表里,事件后面加上分类列,将事件根据自己的需要分类,比如“睡眠”、“工作”、“阅读”和“写作”等。

点击【插入】——【透视表】,即可分析一天的时间情况。

相关文章

网友评论

    本文标题:自动整理时间记录的代码

    本文链接:https://www.haomeiwen.com/subject/htxtdjtx.html