Python Forum

Full Version: Set 'Time' format cell when writing data to excel and not 'custom'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Smile ,

I am using python v3.6 with xlsxwriter. Writing time into excel cell. The problem that I see that the format cell is custom and not Time. I tried using "%H%M%S" in format cell. Also used datetime object and time object instead of string, but in all cases cell format was custom and not Time.
Any idea how can I change it to be Time format cell instead of custom?
BTW, in date I used datetime object with "/" separator and it appeared as date. Maybe I am using wrong format. I tried to use [h]:mm:ss and also hh:mm:ss.

Does anyone has working example that set the time as an time object and not custom?

Thank you for your help ,
Limor
From the docs: https://xlsxwriter.readthedocs.io/
Specifically: https://xlsxwriter.readthedocs.io/workin..._time.html
format7 = workbook.add_format({'num_format': 'mmm d yyyy hh:mm AM/PM'})
worksheet.write('A7', number, format7)       # Feb 28 2013 12:00 PM
(Mar-29-2021, 10:30 AM)Larz60+ Wrote: [ -> ]From the docs: https://xlsxwriter.readthedocs.io/
Specifically: https://xlsxwriter.readthedocs.io/workin..._time.html
format7 = workbook.add_format({'num_format': 'mmm d yyyy hh:mm AM/PM'})
worksheet.write('A7', number, format7)       # Feb 28 2013 12:00 PM

Hi,

Thank you for you reply, appreciate it.
I tried it and it results with general type and not Time format cell Cry . you can look right click -> Format cell and see it is general type and not time
İmage


Thanks,
Limor
here's the example from text, verbatim:
import xlsxwriter

workbook = xlsxwriter.Workbook('date_examples.xlsx')
worksheet = workbook.add_worksheet()

# Widen column A for extra visibility.
worksheet.set_column('A:A', 30)

# A number to convert to a date.
number = 41333.5

# Write it as a number without formatting.
worksheet.write('A1', number)                # 41333.5

format2 = workbook.add_format({'num_format': 'dd/mm/yy'})
worksheet.write('A2', number, format2)       # 28/02/13

format3 = workbook.add_format({'num_format': 'mm/dd/yy'})
worksheet.write('A3', number, format3)       # 02/28/13

format4 = workbook.add_format({'num_format': 'd-m-yyyy'})
worksheet.write('A4', number, format4)       # 28-2-2013

format5 = workbook.add_format({'num_format': 'dd/mm/yy hh:mm'})
worksheet.write('A5', number, format5)       # 28/02/13 12:00

format6 = workbook.add_format({'num_format': 'd mmm yyyy'})
worksheet.write('A6', number, format6)       # 28 Feb 2013

format7 = workbook.add_format({'num_format': 'mmm d yyyy hh:mm AM/PM'})
worksheet.write('A7', number, format7)       # Feb 28 2013 12:00 PM

workbook.close()
and the results:
[attachment=1058]