如何使用python脚本读取XLSX文件的特定列中的数据

时间:2016-08-08 07:35:56

标签: python python-2.7

我想从XLSX文件中读取第2和第3列中给出的数据。

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)

但是当我在脚本上面执行时,会给出以下错误..

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

2 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

此示例读取Excel工作表的所有内容并将其放入矩阵(列表列表)中,然后您可以使用所需的列:

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