Python: Read Excel file with xldr

Posted: June 20, 2016 in Python
Tags:

First make sure you’ve install the xlrd module:

$ sudo pip install xlrd

Then here are the basic code to read the Excel file with Python:

#!/usr/bin/python

import xlrd

def read_file_xlsx(read_file):
    workbook = xlrd.open_workbook(read_file)
    worksheet = workbook.sheet_by_index(int(worksheet_index))
    num_cols = worksheet.ncols # Number of columns
    for row_n in range(0, worksheet.nrows): # Iterate through rows
        print ('Row: %s' % row_n) # Print row number
       for col_n in range(0, num_cols): # Iterate through columns
            cell_value = worksheet.cell(row_n, col_n) # Get cell object by row, col
           print ('Column: [%s] cell_obj: [%s]' % (col_n, cell_value))

    return 'anything'

results = read_file_xlsx('my_file.xlsx');

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s