Python: Reading & Writing to Excel
June 19th, 2009
install ‘xlrd’ for reading…
Download: http://www.lexicon.net/sjmachin/xlrd.htm
root@donkey:~# sudo apt-cache search xlrd
python-xlrd - extract data from MS Excel spreadsheet files
import xlrd
wb=xlrd.open_workbook('/home/jesterj/Desktop/JJ-MonthlyNumbersNew-6.18.09.xls')
wb.sheet_names()
[u'Sheet1', u'Sheet2', u'Sheet3']
sh=wb.sheet_by_index(0)
for rownum in range(sh.nrows):
print sh.row_values(rownum)
Ref: http://scienceoss.com/read-excel-files-from-python/
Install ‘xlwt’ for writing excel files…
Download: http://pypi.python.org/pypi/xlwt
import xlwt
from datetime import datetime
font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True
style0 = xlwt.XFStyle()
style0.font = font0
style1 = xlwt.XFStyle()
style1.num_format_str = 'D-MMM-YY'
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save('example.xls')