Python ile excel dosyalarını okumak


Read Excel files from Python

Published on April 9, 2008 in Python. 32 Comments Tags: , , .

Use the excellent xlrd package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:

Open the workbook

import xlrd
wb = xlrd.open_workbook('myworkbook.xls')

Check the sheet names


Get the first sheet either by index or by name

sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')

Iterate through rows, returning each as a list that you can index:

for rownum in range(sh.nrows):
    print sh.row_values(rownum)

If you just want the first column:

first_column = sh.col_values(0)

Index individual cells:

cell_A1 = sh.cell(0,0).value
cell_C4 = sh.cell(rowx=3,colx=2).value

(Note Python indices start at zero but Excel starts at one)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s