Skip to content
Advertisement

How to output durations in HHHH:MM format with xlsxwriter in Python

I am trying to output durations to an Excel spreadsheet from Python with xslxwriter.

I have the following code:

JavaScript

The spreadsheet that is output contains in its single cell 0:01, i.e. zero hours and one minute. I am trying to get to get it to say 24:01.

I cannot find a way to do this with xslswriter, though there’s no problem representing that value in Excel. I have tried using the write_number method instead with a similar format, but that just outputs zeroes.

So far, the best I can manage is to write the duration as a string, and then convert it to a number in Excel.

Advertisement

Answer

It isn’t an XlsxWriter issue. You just need to specify the correct number format for Excel which in this case is something like [h]:mm. The brackets will give a 24h+ format.

Like this:

JavaScript

Output:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement