Trying to format dates in column with openpyxl

回眸只為那壹抹淺笑 提交于 2020-01-06 04:54:06

问题


sourcefile.xlsx has column "A" with cell using the date format mm/dd/yyyy or 9/10/2019.

When the column "A" cells are copied from source to new destination sheet, the date formatting is changed to 2019-09-10 0:00:00:

I am struggling to identify a way to preserve the date format in the source file mm/dd/yyyy. Is that possible? If not, could I somehow use iter_rows to iterate through the column and set the date?

I have tried:

import datetime

for row in ws3.iter_rows(min_row=6, max_row=None, min_col=1, max_col=1):
    dttm = datetime.datetime.strptime(row,"%m/%d/%Y")
    for cell in row:
        ws3.cell.value = dttm

But that throws the exception:

I am also playing with:

for row in ws3.iter_rows(min_col=1, max_col=1, min_row=7, max_row = None):
    for cell in row:
        cell.number_format = 'Custom'

which fills the columns with 'Cu0to0' so I imagine there must be a way to pass mm/dd/yyyy there but I can't figure it out from openpyxl source code.

来源:https://stackoverflow.com/questions/58441894/trying-to-format-dates-in-column-with-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!