HonorHours/main.py

84 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pandas as pd
import datetime
from math import floor
from copy import copy
import os
# Read & Process ID table
items = os.listdir(os.path.abspath('.'))
print("在当前目录共有以下文件:")
file_index = 0
items_copy = copy(items)
for item in items:
if item != "main.py" and item != "main.exe" and item[0] != '.':
file_index += 1
print(f"{file_index} - {item}")
else:
items_copy.remove(item)
id_index = int(input("请选择身份证列表(输入数字后回车)"))
duty_index = int(input("请选择值班排班表(输入数字后回车)"))
identities_path = r'./' + items_copy[id_index - 1]
duty_path = r'./' + items_copy[duty_index - 1]
print("正在读取身份信息...")
identities = pd.read_excel(identities_path, sheet_name= "Sheet1")
id_dict = {}
index = 0
for name in identities["姓名"]:
id_dict[name] = identities["身份证号码"][index]
index += 1
print("读取完毕!")
# Read & Calculate Honor Hours
print("正在读取值班信息...")
duty = pd.read_excel(duty_path, sheet_name = "Sheet1")
hours_dict = {}
index = 0
time_fmt = "%H:%M"
for members in duty["人员"]:
period = duty["时间"][index].split('-')
time_end = datetime.datetime.strptime(period[1], time_fmt)
time_start = datetime.datetime.strptime(period[0], time_fmt)
hours = (time_end - time_start).seconds / 3600
members = members.split('')
for member in members:
if member not in hours_dict.keys():
hours_dict[member] = hours
else:
hours_dict[member] += hours
index += 1
for member in hours_dict.keys():
hour = hours_dict[member]
hour *= 2
if hour % 1 != 0:
hour = floor(hour)
hour /= 2
hours_dict[member] = hour
print("读取完毕!")
# Generate Honor Hour table
print("正在生成信息填报表格...")
hour_path = r'./hour.xlsx'
id_list = []
for member in hours_dict.keys():
id_list.append(str(id_dict[member]))
df = pd.DataFrame({"姓名": hours_dict.keys(), "身份证号码": id_list, "荣誉时数值": hours_dict.values()})
df = df.set_index("姓名")
df.to_excel(hour_path)
input("生成完毕!在当前文件夹下,文件名为 hour.xlsx按回车键退出...")
# TODO: Write to Document