Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing date format using xlwt

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

like image 784
Antonio Avatar asked Apr 11 '14 15:04

Antonio


People also ask

How does Python write dates in Excel?

If we want YYYY-MM-DD then we specify “%Y-%m-%d”. If we wanted DD/MM/YYYY, then we specify “%d/%m/%Y”. We can literally specify anything like “%d day of %m awesome month of % Y year” will convert all the dates to 24 day of 02 awesome month of 2019 year.

What is XLWT in Python?

This is a library for developers to use to generate spreadsheet files compatible with Microsoft Excel versions 95 to 2003. The package itself is pure Python with no dependencies on modules or packages outside the standard Python distribution.


1 Answers

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)
like image 134
x3al Avatar answered Nov 14 '22 23:11

x3al