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

后端 未结 6 1174
我在风中等你
我在风中等你 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:22

    I'm adding this as a new answer since I don't have enough reputation to add a comment to the above. The simplest way to format a cell is using .number_format = "format" as 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 is tested in openpyxl (2.2.2)

提交回复
热议问题