0
votes

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?

1
Style objects have been deprecated. Please upgrade to a more recent version of openpyxl and use the number_format attribute. - Charlie Clark
I alos tried with that option. Same result. - Yebach
Then please update your code accordingly. openpyxl will automatically apply ISO formats for dates and times. It looks like you're passing in a datetime value, which will be formatted as a datetime. - Charlie Clark

1 Answers

0
votes

So the final catch was also adding the right time format for cells with hours and also to the cell that contains the SUM formula

 _cell.number_format='[h]:mm;@'