def string_to_date_with_xls_validation(cls, date_str): date_obj = datetime.strptime(date_str, '%Y-%m-%d').date() try: # SharedDate().datetime_to_julian(date_obj) # Copy code from v2.0.5. Could not find where SharedDate is in # latest version of openpyxl (and if it's useful) if isinstance(date_obj, datetime): to_excel(date_obj) elif isinstance(date_obj, date): to_excel(date_obj) elif isinstance(date_obj, time): time_to_days(date_obj) elif isinstance(date_obj, timedelta): timedelta_to_days(date_obj) except ValueError: return date_str else: return date_obj
def _cast_datetime(self, value): """Convert Python datetime to Excel and set formatting""" if isinstance(value, datetime.datetime): value = to_excel(value, self.base_date) self.number_format = numbers.FORMAT_DATE_DATETIME elif isinstance(value, datetime.date): value = to_excel(value, self.base_date) self.number_format = numbers.FORMAT_DATE_YYYYMMDD2 elif isinstance(value, datetime.time): value = time_to_days(value) self.number_format = numbers.FORMAT_DATE_TIME6 elif isinstance(value, datetime.timedelta): value = timedelta_to_days(value) self.number_format = numbers.FORMAT_DATE_TIMEDELTA return value
def _bind_timedelta(value, cell): cell.data_type = Cell.TYPE_NUMERIC cell._value = timedelta_to_days(value) cell.number_format = numbers.FORMAT_DATE_TIMEDELTA