From 3c8ad8573213e96862e1acc47e4424fe12e96bf9 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 13 Sep 2017 13:40:49 +0200 Subject: [PATCH] added excel parsing --- arduino_timetable/__init__.py | 77 +++++++++++++++++++++++++++++++--- testprogramm.xlsx | Bin 0 -> 9918 bytes 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 testprogramm.xlsx diff --git a/arduino_timetable/__init__.py b/arduino_timetable/__init__.py index 0ce8e4e..50becf4 100644 --- a/arduino_timetable/__init__.py +++ b/arduino_timetable/__init__.py @@ -6,7 +6,7 @@ import serial.tools.list_ports from collections import namedtuple from datetime import datetime, timedelta - +from openpyxl import load_workbook __version__ = '0.0.1' @@ -77,6 +77,67 @@ def parse_time_table(timetable, available_commands): return timed_commands +def parse_excel_file(path, available_commands): + ''' parses an time table in an excel file into a list of ScheduledCommands + + path: + path to excel file + the time must be in the first column of the first sheet, the command in + the second column + + available_commands: + a dictionary containing the available human readable commands as + keys and the commands to send as values + + {'open': 0, 'close': 1} + + returns a list consisting of ScheduledCommands + ''' + workbook = load_workbook(path, read_only=True) + sheets = workbook.get_sheet_names() + sheet_name = sheets[0] + sheet = workbook[sheet_name] + + is_header = True + timed_commands = [] + for i, row in enumerate(sheet.rows): + delta = parse_time_cell(row[0]) + print(i, row[0].value, delta) + if delta is None: + # no time in the cell + if is_header: + # this is still a header + continue + else: + # not the header and not a time cell + # premature end of list + break + else: + # a time in the cell, this is not a header any more + is_header = False + raw_command = row[1].value + cmd = parse_command(raw_command, available_commands) + # add a ScheduledCommand to the resulting list + tc = ScheduledCommand(delta, raw_command, cmd) + timed_commands.append(tc) + return timed_commands + + + +def parse_time_cell(excel_cell): + if excel_cell.is_date: + return time_as_timedelta(excel_cell.value) + if isinstance(excel_cell.value, float): + return timedelta(days=excel_cell.value) + try: + delta = parse_time(excel_cell.value) + return delta + except ValueError: + return None + + + + def parse_time(time_str): ''' parses a string to extract the time information @@ -95,14 +156,18 @@ def parse_time(time_str): else: msg = "time data '{}' does not match any format".format(time_str) raise ValueError(msg) + return time_as_timedelta(time_obj) + + +def time_as_timedelta(time_object): + ''' converts a time object to a timedelta ''' return timedelta( - hours=time_obj.hour, - minutes=time_obj.minute, - seconds=time_obj.second, - microseconds=time_obj.microsecond + hours=time_object.hour, + minutes=time_object.minute, + seconds=time_object.second, + microseconds=time_object.microsecond ) - def parse_command(command, available_commands): ''' parses a string and checks if it is a valid command diff --git a/testprogramm.xlsx b/testprogramm.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..0f20a200e3e8f746a65d68284b7dedafd5cfe3d1 GIT binary patch literal 9918 zcmeHtbyQRfyZ6u_Al*oJ4&B`y0@97dFtmh(bhmU%cbC#7(ukC_l!UZ&eWT~R*K;`D zZ@p{X|L%SFtl4|*cz!ec*}tc^ssaowHUJ)g2mk=c0Y! zE_{u$<7mJmQ6|j!V-)PW_SdSaB@?oo#4qI%`s~v!Y1hCyl0=AB8A^R*`w@t?oAotA z8m(i7FV88&)wRmHb!7VVjq@2hgCtQ0u$>ZGYqpI!C9!a}G!uu(nu4fY0oS`^ z&(Sg^CyjY<1`h&dTcE2srb?EDw*20bF9G1PEcZaZY*po!zHg6ayT`K(JZVT-$~Q+V zSTg0Of}1toleq9C`Z97NjpnU)zrY1(=5cLhS;``sucYW;@!(Y-8_wWbcoVr!JLpDH z`sl%<^4r~qMeny9+j|&SLT}L@$fbD~K@&)M3OS<)F*szvD3p! ztY7cg$s)cfXi=1HA=e=3q}nHKh{~f9JwFSC@s~H7|9JLwOhj*Kf74onl-QJ2x#u07 zSPh4%=ZuYeMYa}NjwywuHFiH?G_lFE#U)!K_xyx()0yky+K33kcL_I?rkqbyu#u0x zITgeK)do$hKE5U>wpd8m5DM-fQDqjK>asyZ@I)vTz7o8wZQ@k2cW2%7HceKJ6wSgC z7;@c^c#d2$HeZ}3J|q`_bCU51W8j*?3?_?aPG9BweIu_Ug+kc4?8=GYa=5eT?yet8 zwpGbR+o<4bQQ#LP^9z%%J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