Python Excel Example

Do you like this?

Summary:
This article shows you how to handle excel files using Python


Content:

If you are using Windows, you can use PyWin32 which is a wrapper around Excel COM library. You can check out Python Inserting Images Into Word Documents for examples on how to use the library. If you are using Macbook or Linux, you can use Python Excel. In this tutorial, I'll be talking about the second option which is Python Excel. Python Excel comprises of 2 libraries. xlrd is for reading Excel files and xlwt is for writing data to Excel files. Following are examples on how to use them.

wget https://pypi.python.org/packages/source/x/xlrd/xlrd-0.9.3.tar.gz
tar -zxvf xlrd-0.9.3.tar.gz
cd xlrd-0.9.3
python setup.py install

wget https://pypi.python.org/packages/source/x/xlwt/xlwt-0.7.5.tar.gz
wget https://pypi.python.org/packages/source/x/xlutils/xlutils-1.7.1.tar.gz
from xlrd import open_workbook
wb = open_workbook('/path/to/file')
for s in wb.sheets():
   print s.name

Open an existing excel file to edit

from xlrd import open_workbook
from xlutils.copy import copy
from xlwt import easyxf

excel_file= "\path\to\excel\file.xls"

rb = open_workbook(excel_file)
book = copy(rb)
sheet = book.get_sheet(0)
col=0
row= 0
for v in ['a','b','c']:
	sheet.write(row,col,file_)
	row +=1

book.save(excel_file)

 
comments powered by Disqus