Python XLWT sometimes saves file of 0 bytes size

89 views Asked by At

I have a simple function

def put_to_xls(*args)

which works on Raspberry and it should take some data from *args and save it to *.xls-file then. In 99% of time it works well, but sometimes is saves a file with a right name but with a size of 0 bytes. Maybe it happens because sometimes Raspberry can turn off due to power loss. Maybe something wrong with my code. But what I don't understand is how is it possible? Why I can get the file of 0 byte size, though it has a name? And how to fix it?

def make_worksheet(date, shift, wb):
    ws = wb.add_sheet(f'Date {date} shift {shift}')
    return ws


def make_xls(date, shift):
    wb = xlwt.Workbook(encoding='utf-8')
    ws = make_worksheet(date=date, shift=shift, wb=wb)
    font_style = xlwt.XFStyle()
    font_style.font.bold = True
    headers = ['N', 'Date', 'Start',
               'Finish', 'Duration']
    for cols, header in enumerate(headers):
        ws.write(0, cols, header, font_style)
    rows_num = 1
    return wb, ws, rows_num


def save_file(date, shift, wb):
    wb.save(os.path.join(
        BASE_DIR, f'reports/{date}-{EQUIP_NAME}-{shift}.xls'))


def put_to_xls(date, shift, operations):
    wb, ws, rows_num = make_xls(date=date, shift=shift)
    font_style = xlwt.XFStyle()
    for operation in operations:
        op_date, start, finish = operation
        delta = op_length(start, finish)
        hours, minutes, seconds = timedelta_to_hms(delta)
        data = [rows_num, op_date, start[11:19], finish[11:19],
                f'{hours} h, {minutes} min, {seconds} sec.']
        for cols, header in enumerate(data):
            ws.write(rows_num, cols, header, font_style)
            ws.col(cols).width = (256 * 12) if cols == 0 else (256 * 28)
        rows_num += 1
    save_file(date=date, shift=shift, wb=wb)
    return date, shift
0

There are 0 answers