2019年1月20日 星期日

[其他] 體重及體脂率記錄表

寫了一個小 python script
把置底的體重記錄文字資料
轉成 excel 檔案
這樣就能用 excel 來畫折線圖了

2017.10.03 開始減肥到現在 2019.01.18 的體重變化折線圖
https://imgur.com/pdmE9ZV

2007.05.07 到現在 2019.01.18 的 11 年半體重變化折線圖
https://imgur.com/Nq7owXa

https://imgur.com/a/305Z9CJ


#! python3

import re, os, sys, openpyxl, datetime

if os.path.exists(sys.argv[1]):
    fname = os.path.splitext(sys.argv[1])
    weight_file = open(sys.argv[1],'r')
    weight_lines=weight_file.readlines()
    print(weight_lines[0])
    weightRegex = re.compile(r'(\d\d\d\d)\.(\d\d)\.(\d\d)\s+(\w+)\s+(\d\d\.\d)kg')
    weightNoYRegex = re.compile(r'(^\s+)(\d\d)\.(\d\d)\s+(\w+)\s+(\d\d\.\d)kg')
    wb = openpyxl.Workbook()
    sheet = wb.active
    rows = 1;
    year = 2019;
    year_str = '2019';
    year_flag = 0;
    for i in range(0,len(weight_lines)):
        weight_group = weightRegex.search(weight_lines[i])
        weightNoY_group = weightNoYRegex.search(weight_lines[i])
        if weightRegex.search(weight_lines[i]):
            year = weight_group.group(1)
            year_str = year
            if weight_group.group(2) != '12':
                year_flag = 0;
            date_string = weight_group.group(1) + '.' + weight_group.group(2) + '.' + weight_group.group(3)
            print(date_string)
            date_time = datetime.datetime.strptime(date_string, '%Y.%m.%d')
            sheet.cell(row=rows,column=1). value = date_time
            sheet.cell(row=rows,column=1). number_format = 'yyyy-mm-dd'
            sheet.cell(row=rows,column=2). value = float(weight_group.group(5))
            rows=rows+1
        elif weightNoYRegex.search(weight_lines[i]):
            if weightNoY_group.group(2) == '12':
                if year_flag == 0:
                    year_int = int(year) - 1;
                    year_flag = 1;
                year_str = str(year_int)
            else:
                year_flag = 0;
            date_string = year_str + '.' + weightNoY_group.group(2) + '.' + weightNoY_group.group(3)
            print(date_string)
            date_time = datetime.datetime.strptime(date_string, '%Y.%m.%d')
            sheet.cell(row=rows,column=1). value = date_time
            sheet.cell(row=rows,column=1). number_format = 'yyyy-mm-dd'
            sheet.cell(row=rows,column=2). value = float(weightNoY_group.group(5))
            rows=rows+1
    wb.save(fname[0] + '.xlsx')
    weight_file.close()
else:
    print("File does not exist!!")

沒有留言:

張貼留言