Home > database >  data time Format recognition in exported excel with xlsxwriter
data time Format recognition in exported excel with xlsxwriter

Time:05-07

I didn't find a solution for this:

From a dataframe I generate an excel and some columns need to be in format hh:mm:ss (with no limit to 24h, for example a value can be '28:39:13'.

When generating the excel everything looks okay. But when operating with the values of the cells isn't working properly until I apply the "text to columns" option.

## Example code
import pandas as pd
import numpy as np
from xlsxwriter.utility import xl_rowcol_to_cell
example_list = ["12:35:25", "", "", "", "27:36:11"]
example_df = pd.DataFrame(example_list)
writer = pd.ExcelWriter('enhanced.xlsx', engine='xlsxwriter')
example_df.to_excel(writer, index=False, header=False, sheet_name='report')
workbook = writer.book
worksheet = writer.sheets['report']
total_hformat = workbook.add_format({'num_format': '[h]:mm:ss', 'bold': False})
worksheet.set_column('A:C', 12, total_hformat)
writer.save()
writer.close()

Before applying "text to columns":

enter image description here

Apart from the for() loop the rest of the code is the same as yours. I've allowed for decimal seconds which may not be necessary.

  • Related