1

我想导入一个EXCEL文件,但是代码有问题。求助!

import xlrd

fname = "D:/pdf-ex/exc.xls"
bk = xlrd.open_openwork("fname","rb")
shxrange = range(bk.nsheets)
sh = bk.sheet_by_name("Sheet1")
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)

cell_value = sh.cell_value(1,1)
print cell_value

row_list = []
for i in range(1,nrows):
    row_data = sh.row_values(i)
    row_list.append(row_data)
4

1 回答 1

2

The reason for that is because xlrd module does not have open_openwork() function in it.

The function you may be looking for is open_workbook():

open_workbook(...) - Open a spreadsheet file for data extraction.

Plus there is nothing about the mode in the docs - by providing second argument you set the log file (opened file object to which messages will be read).

So instead of:

bk = xlrd.open_openwork("fname","rb")

do:

bk = xlrd.open_workbook("fname")
于 2012-05-25T01:32:48.803 回答