From 8f74371dedcd39791b0a3f7fe618b16e2698c8b6 Mon Sep 17 00:00:00 2001 From: OliE Date: Thu, 22 May 2025 11:14:15 +0200 Subject: [PATCH] Updated Useful import scripts (markdown) --- Useful-import-scripts.md | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/Useful-import-scripts.md b/Useful-import-scripts.md index 2b589b3..3cf1402 100644 --- a/Useful-import-scripts.md +++ b/Useful-import-scripts.md @@ -198,3 +198,62 @@ with open('Daily activity metrics.csv', newline='') as input_csv: if (weight and date): output_writer.writerow([parse(date).strftime('%d.%m.%Y 08:00'), round(float(weight), 2)]) ``` + +## Apple Health --> openScale CSV file + +by MoralCode see https://github.com/oliexdev/openScale/issues/731 + +```python +#!/usr/bin/python + +# Usage: first run your apple health export through the scripts as documented in https://github.com/markwk/qs_ledger/tree/master/apple_health +# then run this script using the BodyMass.csv from this process to get an OpenScale csv + +import argparse +import csv +import datetime +from dateutil.parser import parse as parsedate + +OPENSCALE_HEADER = '"biceps","bone","caliper1","caliper2","caliper3","calories","chest","comment","dateTime","fat","hip","lbm","muscle","neck","thigh","visceralFat","waist","water","weight"' +OPENSCALE_HEADER = OPENSCALE_HEADER.replace('"', '') + +_APPLE_QUANTIFIEDSELF_BODYMASS_HEADER = 'sourceName,sourceVersion,device,type,unit,creationDate,startDate,endDate,value' + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('apple_bodymass_csv_file_path') + parser.add_argument('output_path') + + args = parser.parse_args() + + with open(args.apple_bodymass_csv_file_path, 'r') as inp: + reader = csv.DictReader(inp) + with open(args.output_path, 'w') as outp: + writer = csv.DictWriter(outp, OPENSCALE_HEADER.split(',')) + # outp.write(f'{OPENSCALE_HEADER}\n') + writer.writeheader() + + for line in reader: + creationDate = line['creationDate'] + output_date = parsedate(creationDate).strftime('%Y-%m-%d %H:%M') + weight = float(line['value']) + if line['unit'] == 'lb': + # convert to KG + weight = weight / 2.2 + + comment = "Imported from Apple Health export. Data originally from " + appname = line["sourceName"] + # make the apple health app name a little more obvious. by default its just "health" + if appname == "Health": + appname = "Apple Health" + comment += appname + " app" + appversion = line["sourceVersion"] + if appversion != "": + comment += " version " + appversion + + writer.writerow({ + 'dateTime': output_date, + 'weight': "{:.2f}".format(weight), + 'comment': comment + }) +``` \ No newline at end of file