Friday, 15 July 2011

python - How to format cell with datetime object of the form 'yyyy-mm-dd hh:mm:ss' in Excel using openpyxl -



python - How to format cell with datetime object of the form 'yyyy-mm-dd hh:mm:ss' in Excel using openpyxl -

so, given:

dttm = datetime.datetime.strptime("2014-06-23 13:56:30", "%y-%m-%d %h:%m:%s") ws['a1'] = dttm

the result in excel right date-time written cell (you can see you'd input formulas). but, cell display format mm/dd/yyyy.

i need cell display "6/23/2014 13:56" instead of "6/23/2014".

how can explicitly format cell accomplish this?

thanks!

edit

@alecxe solution works , asked for. able save styles solution @woodham. unfortunately raises typeerror (see comment). suggestions?

i'm adding new reply since don't have plenty reputation add together comment above. simplest way format cell using .number_format = "format" in:

value = datetime.datetime.strptime("2014-06-23 13:56:30", "%y-%m-%d %h:%m:%s") cell = ws['a1'] cell.value = value cell.number_format = 'yyyy mmm dd'

this tested in openpyxl (2.2.2)

python excel openpyxl

No comments:

Post a Comment