Pkde
Pkde

Reputation: 35

How to read data in specific column of XLSX file using python script

I want to read the data given in 2nd and 3rd column from XLSX file.

import xlrd

workbook = xlrd.open_workbook("C:/Users/File.xlsx","rb")
sheet = workbook.sheet_by_index(0)
for row in range(sheet.nrows):
    cols = (sheet.row_values(row,1)) and (sheet.row_values(row,2))
    print(cols)

But is gives below error when i executed above script..

biff_version = bk.getbof(XL_WORKBOOK_GLOBALS) File
C:\Python27\.....\xlrd_init_.py", line 1323, in getbof raise
XLRDError('Expected BOF record; found 0x%04x' % opcode)
xlrd.biffh.XLRDError: Expected BOF record; found 0x4b50

Upvotes: 2

Views: 21122

Answers (2)

Trimax
Trimax

Reputation: 2473

This example read all the content of the excel sheet and puts it in a matrix (list of lists), then you can use the columns you need:

import xlrd

workbook = xlrd.open_workbook("C:/Users/File.xlsx","rb")
sheet = workbook.sheet_by_index(0)
rows = []
for i in range(sheet.nrows):
    columns = []
    for j in range(sheet.ncols):
        columns.append(sheet.cell(i, j).value)
    rows.append(columns)
print rows

Upvotes: 1

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11705

Try this

import xlrd

workbook = xlrd.open_workbook("C:/Users/File.xlsx","rb")
sheets = workbook.sheet_names()
required_data = []
for sheet_name in sheets:
    sh = workbook.sheet_by_name(sheet_name)
    for rownum in range(sh.nrows):
        row_valaues = sh.row_values(rownum)
        required_data.append((row_valaues[0], row_valaues[1]))
print required_data

Upvotes: 4

Related Questions