Python Forum
Set 'Time' format cell when writing data to excel and not 'custom'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set 'Time' format cell when writing data to excel and not 'custom'
#1
Smile 
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
Reply
#2
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
Reply
#3
(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
Reply
#4
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:
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python date format changes to date & time 1418 4 516 Jan-20-2024, 04:45 AM
Last Post: 1418
  Export data from PDF as tabular format zinho 5 646 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 1,925 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Set string in custom format korenron 4 1,048 Jan-16-2023, 07:46 PM
Last Post: mutantGOD
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,802 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 2,874 Dec-06-2022, 11:09 AM
Last Post: mg24
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,620 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Appending a row of data in an MS Excel file azizrasul 3 1,138 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Deleting rows based on cell value in Excel azizrasul 11 2,476 Oct-19-2022, 02:38 AM
Last Post: azizrasul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020