如何用python读取excel文件
发布网友
发布时间:2022-03-03 17:10
我来回答
共3个回答
热心网友
时间:2022-03-03 18:39
以下代码调试通过:
import xlrd
# 打开 xls 文件
book = xlrd.open_workbook("test.xls")
print "表单数量:", book.nsheets
print "表单名称:", book.sheet_names()
# 获取第1个表单
sh = book.sheet_by_index(0)
print u"表单 %s 共 %d 行 %d 列" % (sh.name, sh.nrows, sh.ncols)
print "第二行第三列:", sh.cell_value(1, 2)
运行效果:
热心网友
时间:2022-03-03 19:57
import xlrd, xlutils
from xlrd import open_workbook
from xlutils.copy import copy
from xlutils.filter import process,XLRDReader,XLWTWriter
def copy2(wb):
w = XLWTWriter()
process(
XLRDReader(wb,'unknown.xls'),
w
)
return w.output[0][1], w.style_list
inBook = xlrd.open_workbook(r"Book1.xls", formatting_info=True, on_demand=True)
inSheet = inBook.sheet_by_index(0)
# Copy the workbook, and get back the style
# information in the `xlwt` format
outBook, outStyle = copy2(inBook)
# Get the style of _the_ cell:
xf_index = inSheet.cell_xf_index(0, 0)
saved_style = outStyle[xf_index]
saved_style.font.colour_index = 11
#Update the cell, using the saved style as third argument of `write`:
outBook.get_sheet(0).write(0,0,'changed!', saved_style)
outBook.save(r"Book2.xls")
热心网友
时间:2022-03-03 21:32
可以通过pip包管理器来安装Python包,但是pygame包包含C语言代码,需要进行编译。