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

后端 未结 6 1140
我在风中等你
我在风中等你 2021-01-02 06:57

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 is that

6条回答
  •  孤街浪徒
    2021-01-02 07:18

    I found that this worked. Although number_format is used it seems to recognise the date format specified when put into the excel wb.

    import datetime
    date = datetime.date(2020, 2, 24) # python datetime format is yyyy mm dd
    ws.cell(row=[row_ref], column=[col_ref], value=date)
    ws.cell(row=[row_ref], column=[col_ref]).number_format = 'dd/mm/yy'
    

提交回复
热议问题