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

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

    For openpyxl 2.4.5 you'll no longer have access to NumberFormat and Style and will have to use NamedStyle. Here's some sample usage:

    from openpyxl.styles import NamedStyle
    
    date_style = NamedStyle(name='datetime', number_format='DD/MM/YYYY HH:MM:MM')
    ws['A1'].style = date_style
    

    Alternatively with the new NamedStyle class, you can set the style by the string name once NamedStyle has been instantiated:

    from openpyxl.styles import NamedStyle
    
    NamedStyle(name='custom_datetime', number_format='DD/MM/YYYY HH:MM:MM')
    ws['A1'].style = 'custom_datetime'
    

    Documentation here: https://openpyxl.readthedocs.io/en/stable/styles.html

提交回复
热议问题