[Python] python openpyxl time insert without date

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a value that I want to insert into excel as time formatted like HH:MM If I use

    cellFillTime = Style(fill=PatternFill(start_color=shiftColor, end_color=shiftColor, fill_type='solid'),
    border=Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'),
    bottom=Side(style='thin')),
    alignment=Alignment(wrap_text=True),
    number_format='HH:MM'
    )
    valM = 8

    cellData = ws5.cell(column=a + 3, row=i + 2, value=valM)
    _cellStlyed = cellData.style = cellFillTime


    I always get 1.1.1900 8:00:00 in excel worksheet

    The problem I get is when I have SUM functions later and therefore they do not work.

    How can I remove date from my cell when formatting cell to get only time values

    thank you

    best regards

    This worked in terms of only hour insert

    UPDATED CODE

    cellFillTime = Style(fill PatternFill(start_color=shiftColor,end_color=shiftColor,fill_type='solid'),
    border=Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),
    bottom=Side(style='thin')),
    alignment=Alignment(wrap_text=True)
    )

    if rrr["rw_seqrec"] == 1 or rrr["rw_seqrec"] == 1001:
    val_ = abs((rrr['rw_end'] - rrr['rw_start'])) / 60
    #print "val_ ", val_
    valM = datetime.datetime.strptime(str(val_), '%H').time()
    cellData = ws5.cell(column= a + 3, row= i+2, value=valM)
    cellData.style = cellFillTime
    cellData.number_format='HH:MM'



    the problem I have now is that excel still does not want to sum the time fields. It has smth to do with field format or smth.

    any suggestions?

    Continue reading...

Compartilhe esta Página