0% found this document useful (0 votes)
88 views8 pages

Python and Excel: Chris Withers

The document discusses using Python to read from, write to, and modify Excel files. It recommends using the xlrd library to read Excel files, xlwt to write Excel files, and xlutils to modify existing Excel files. It provides code examples for reading data and cell values from Excel files using xlrd, writing data to new Excel files using xlwt, and modifying an existing Excel file using xlutils. It also provides links to find documentation and support for these Python Excel libraries.

Uploaded by

Phuong Le
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views8 pages

Python and Excel: Chris Withers

The document discusses using Python to read from, write to, and modify Excel files. It recommends using the xlrd library to read Excel files, xlwt to write Excel files, and xlutils to modify existing Excel files. It provides code examples for reading data and cell values from Excel files using xlrd, writing data to new Excel files using xlwt, and modifying an existing Excel file using xlutils. It also provides links to find documentation and support for these Python Excel libraries.

Uploaded by

Phuong Le
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python and Excel

Chris Withers

What do people do?

CSV? HTML hack?! COM? NO!

[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

PLEASE DO NOT USE PYEXCELERATOR!


[email protected] http://www.simplistix.co.uk

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

Where can I find these?

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

You might also like