简体   繁体   English

Python Openpyxl将日期写为Excel作为短日期

[英]Python Openpyxl writing date to excel as short date

I am trying to write dates to an excel file using Openpyxl. 我正在尝试使用Openpyxl将日期写入excel文件。 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. 这会将日期写入excel,但格式错误。 This is the output: 这是输出:

2018-01-09 0:00:00 2018-01-09 0:00:00

I am trying to get it to be 1/9/2018. 我正在尝试使其成为1/9/2018。 Basically change the format to Short Date in excel. 基本上将格式更改为Excel中的“短日期”。

Anyone know how to change it before the date is written to excel? 有人知道在将日期写入excel之前如何更改它吗?

In Excel you always have to provide your own format for dates and times, because these are stored as serials. 在Excel中,您始终必须提供自己的日期和时间格式,因为这些日期和时间存储为序列号。 openpyxl defaults to ISO formats for minimal ambiguity. openpyxl默认使用ISO格式,以减少歧义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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