Python
python2.7

FixationAsset(????)自動でexcel形式に

all置換

#coding:utf-8

import sys
from collections import OrderedDict
from collections import defaultdict
from datetime import datetime as dt

japan_list = [u"B331"]
oversea_list = [u"B344",u"B346",u"B352",u"B359"]
asean_list = [u"B344",u"B346",u"B352"]
euro_list = [u"B359"]
entry_list = [u"B344",u"B346",]
global_list = [u"B352",u"B359"]
common_list = [u"B331",u"B344",u"B346",u"B352",u"B359",]

def calc_hours(hour):
    minus_1hour_flag = False    #お昼休憩挟んでるフラグ
    lunch_start = dt.strptime("12:00", '%H:%M')
    lunch_end = dt.strptime("13:00", '%H:%M')

    minus_comma5hour_flag = False   #残業前休憩挟んでるフラグ
    zangyo_rest_start = dt.strptime("17:00", '%H:%M')
    zangyo_rest_end = dt.strptime("17:30", '%H:%M')

    (start_hour_str, end_hour_str) = hour.split(u"-")
    start_hour_str = add_comma_to_hour(start_hour_str)
    end_hour_str = add_comma_to_hour(end_hour_str)
    start_hour = dt.strptime(start_hour_str, '%H:%M')
    end_hour = dt.strptime(end_hour_str, '%H:%M')
    if (start_hour <= lunch_start) and (lunch_end <= end_hour):
        minus_1hour_flag = True #お昼休みが含まれてるなら1時間引かないといけないからフラグ立てる
    if (start_hour <= lunch_start) and (lunch_end <= end_hour):
        minus_comma5hour_flag = True    #残業前休憩が含まれてるなら30分引かないといけないからフラグ立てる

    left_hour = end_hour - start_hour   #時間差
    left_second = left_hour.seconds #秒差
    left_second_by_commma_five = convert_second_to_commma_five(left_second) #秒差→0.5時間刻みの時間差
    if minus_1hour_flag == True:
        left_second_by_commma_five -= 1
    if minus_comma5hour_flag == True:
        left_second_by_commma_five -= 0.5
    return left_second_by_commma_five

def add_comma_to_hour(hour):
    comma_hour = hour[:2] + u":" + hour[2:]
    return comma_hour

def convert_second_to_commma_five(second):
    hour = second/60.0/60.0
    hour_by_commma_five = int(hour*2)/2.0
    return hour_by_commma_five

def convert_hour_to_commma_five(hour):
    hour_by_commma_five = int(hour*2)/2.0
    return hour_by_commma_five

def categorize_codes(atask):
    codes_other_flag = False
    codes_list = []
    if atask.startswith(u"日本"):
        codes_list = japan_list
    elif atask.startswith(u"海外"):
        codes_list = oversea_list
    elif atask.startswith(u"ASEAN"):
        codes_list = asean_list
    elif atask.startswith(u"欧州"):
        codes_list = euro_list
    elif atask.startswith(u"Entry"):
        codes_list = entry_list
    elif atask.startswith(u"グローバル"):
        codes_list = global_list
    elif atask.startswith(u"共通"):
        codes_list = common_list
    else:
        codes_other_flag = True

    return codes_other_flag, codes_list


kotei_shisan_memo="""
0320
0840-1000   配置検討
1000-1200   リワーク
1300-1700   配置検討

0321
休み

0322
休み  研修

0323
0840-1000   配置検討
1000-1200   all検討
1300-1700   配置検討

0324
0840-1200   共通回転数検討
1300-1700   ASEANP検討
1730-2000   グローバルCT


0325
0840-1400   リワーク
1400-1800   室内機リワーク

"""


task_dic = OrderedDict()
task_dic[u"B331"] = u""
task_dic[u"B344"] = u""
task_dic[u"B346"] = u""
task_dic[u"B352"] = u""
task_dic[u"B359"] = u""
task_dic[u"all"] = u""
task_dic[u"BL"] = u""
task_dic[u"その他"] = u""

hour_dic = defaultdict(lambda:0)
total_hour_dic = defaultdict(lambda:0)

f_spaceline = False
f_show = False
for aline_str in kotei_shisan_memo.split("\n"):
    aline = aline_str.decode("utf-8")
    if len(aline)<2:    #空行
        f_spaceline=True
    elif aline.startswith(u"休み"):   #休み
        f_spaceline = False
        f_show = False
        print "―      ―     ―     ―     ―     ―     ―     ―     "
    elif len(aline)<5:  #日付
        f_spaceline = False
        f_show = False
    else:   #作業
        f_spaceline = False
        f_show = True
        (hours, task) = aline.split("\t")
        hour_dic[task] += calc_hours(hours) #各作業の合計時間入れていく

    if (f_spaceline and f_show):    #休み以外の1日分の処理が終わったら、出力
        f_spaceline = False
        f_show = False
        #消費時間の辞書から、各部品コードの作業と時間の辞書作る
        other_flag = True   #task_dicのどれかのキーに当てはまったら、その他に入れるためのフラグ折る
        for key_task,value_hours in hour_dic.items():
            for key_code in task_dic.keys():
                if key_task.startswith(key_code):
                    other_flag = False
                    task_dic[key_code] += key_task + str(value_hours).decode("utf-8")+u"h,"
                    total_hour_dic[key_code] += value_hours #各欄の合計時間
                elif key_task.startswith(u"休み"):
                    other_flag = False
                    pass
                else:
                    codes_other_flag, codes_list = categorize_codes(key_task)   #部品コードとか以外の表現に合致させる
                    if codes_other_flag == False:
                        other_flag = False
                        if key_code in codes_list:
                            task_dic[key_code] += key_task + str(convert_hour_to_commma_five(value_hours*1.0/len(codes_list))).decode("utf-8")+u"h,"
                            total_hour_dic[key_code] += convert_hour_to_commma_five(value_hours*1.0/len(codes_list))    #各欄の合計時間

            if other_flag == True:
                task_dic[u"その他"] += key_task + str(value_hours).decode("utf-8")+u"h,"
                total_hour_dic[u"その他"] += value_hours #その他作業の合計時間

        for k,v in task_dic.items():    #表示
            if total_hour_dic[k] == 0:
                print v + u"\t" +  u"\t" ,
            else:
                print v + u"\t" + str(total_hour_dic[k]) + u"\t" ,
        print
        for k in task_dic.keys():   #クリア
            task_dic[k] = u""
        hour_dic.clear()
        total_hour_dic.clear()

sys.exit()

f_spaceline = False
for aline_str in kotei_shisan_memo.split("\n"):
    aline = aline_str.decode("utf-8")
    if len(aline)<2:    #空行
        f_spaceline=False
    elif aline.startswith(u"休み"):   #休み
        print "―      ―     ―     ―     "
    elif len(aline)<5:  #日付
        f_spaceline=True
    else:   #作業
        (hours, task) = aline.split("\t")
        hour_dic[task] += calc_hours(hours)