I have been using pyExcelerator library for a while, it works nicely. Now, I would like to be able to export python dates as Date format in the excel sheet. I cannot find out (I read the documentation) how to do. Any suggestions ?
Option 1:
Just convert string to datetime type you've got from parsing. Here I assume that you have fixed date format:
import csv
from datetime import datetime
date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
rows = ['foo', 'bar', date_object]
with open('export.csv', 'wb') as csv_file:
writer = csv.writer(csv_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
writer.writerow(rows)
Option 2
Use xlwt module. Example:
#!/usr/bin/env python
# -*- coding: windows-1251 -*-
# Copyright (C) 2005 Kiseliov Roman
from xlwt import *
from datetime import datetime
w = Workbook()
ws = w.add_sheet('Hey, Dude')
fmts = [
'M/D/YY',
'D-MMM-YY',
'D-MMM',
'MMM-YY',
'h:mm AM/PM',
'h:mm:ss AM/PM',
'h:mm',
'h:mm:ss',
'M/D/YY h:mm',
'mm:ss',
'[h]:mm:ss',
'mm:ss.0',
]
i = 0
for fmt in fmts:
ws.write(i, 0, fmt)
style = XFStyle()
style.num_format_str = fmt
ws.write(i, 4, datetime.now(), style)
i += 1
w.save('dates.xls')
More examples: https://github.com/python-excel/xlwt/tree/master/xlwt/examples