11/11/2010

Converting a date value read from a calc cell into readable format using python

When a date value, such as "2010-10-12" or etc., is in a cell,

oCell.getValue() and oCellRange.DataArray methods give just a number, not date format,
while oCell.getString() gives a date format string

The code below shows how to convert a date value into date format.


#!/usr/bin/python
import datetime, uno

# .... some lines to get oCell

# cell value shown in calc -> 2010-10-12
dateValue = oCell.getValue()
# get date value from a cell with getValue() method,
# note that I did NOT used the getString() method.

print dateValue # this gives 40463.0 <- This means the elapsed date from 1899-12-30

DateValue = datetime.timedelta(dateValue) + datetime.date(1899, 12, 30) # converting
# DateValue -> datetime.date(2010, 10, 12)

print DateValue # this gives 2010-10-12.



* The starting date 1899-12-30 can be changed in option > openoffice Calc > calculate > date

댓글 없음: