Python and Excel: Chris Withers
Python and Excel: Chris Withers
Chris Withers
[email protected] http://www.simplistix.co.uk
Reading
xlrd
import xlrd book = xlrd.open_workbook("myfile.xls") print "The number of worksheets is", book.nsheets print "Worksheet name(s):", book.sheet_names() sh = book.sheet_by_index(0) print sh.name, sh.nrows, sh.ncols print "Cell D30 is", sh.cell_value(rowx=29, colx=3) for rx in range(sh.nrows): print sh.row(rx)
[email protected] http://www.simplistix.co.uk
Writing
DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR DO NOT USE PYEXCELERATOR
[email protected] http://www.simplistix.co.uk
Writing
Writing
xlwt
import xlwt from datetime import datetime style0 = xlwt.easyxf( 'font: name Times New Roman, colour red, bold on' ) style1 = xlwt.easyxf('',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, 1, 1) ws.write(2, 2, xlwt.Formula("A3+B3")) wb.save('example.xls')
[email protected] http://www.simplistix.co.uk
Modifying
xlutils.copy
from xlrd import open_workbook from xlutils.copy import copy rb = open_workbook('source.xls',formatting_info=True) wb = copy(rb) ws = wb.get_sheet(0) ws.write(0,0,I'm only changing cell A1) wb.save('output.xls')
[email protected] http://www.simplistix.co.uk
http://pypi.python.org/pypi/xlrd
google for xlrd
http://pypi.python.org/pypi/xlwt
google for xlwt
http://pypi.python.org/pypi/xlutils
google for xlutils
http://groups.google.com/group/python-excel
google for python excel group
[email protected] http://www.simplistix.co.uk