简体   繁体   中英

Python Openpyxl writing date to excel as short date

I am trying to write dates to an excel file using Openpyxl. I am using the following lines to write the date.

dttm = datetime.datetime.strptime(ls25Dict[cell.value][2], "%m/%d/%Y" )   
ws1['B'+ str(cell.row)].value = dttm

This writes the date to excel but in the wrong format. This is the output:

2018-01-09 0:00:00

I am trying to get it to be 1/9/2018. Basically change the format to Short Date in excel.

Anyone know how to change it before the date is written to excel?

In Excel you always have to provide your own format for dates and times, because these are stored as serials. openpyxl defaults to ISO formats for minimal ambiguity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM