问题
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