5

I'm writing a date using xlwt like this:

date_format = XFStyle()
date_format.num_format_str = 'dd/MM/yyyy'
plan.write(1,4,'01/01/2014', date_format)

The worksheet is saving fine, with no errors. But even this cell is formatted as date, excel not recognized it as a date until I manually double click it on the excel. But I have more than 1.000 dates and I can't do that all the time. Is there a way to save as real date, with no need to manually update the cell?

Thanks

Antonio
  • 482
  • 1
  • 5
  • 16
  • Does this answer your question? [Writing xlwt dates with Excel 'date' format](https://stackoverflow.com/questions/17069694/writing-xlwt-dates-with-excel-date-format) – MackM Jan 31 '20 at 15:57

1 Answers1

10

Excel date is a float number in a cell formatted as a date.

You're trying to write a string into cell. xlwt should convert a datetime object to float but it's not going to implicitly convert a string to Excel date.

from datetime import datetime

date_format = XFStyle()
date_format.num_format_str = 'dd/MM/yyyy'
plan.write(1, 4, datetime.strptime("01/01/2014", "%d/%M/%Y"), date_format)
x3al
  • 586
  • 1
  • 8
  • 24