如何使用Python将一行Excel工作表复制到另一个工作表

2024-04-26 11:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
2条回答

请参考python excel库xlrd(用于excel阅读)/xlwt(用于excel编写) http://www.python-excel.org/

例如(读取)(from this):

import xlrd

fname = "sample.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
    sh = bk.sheet_by_name("Sheet1")
except:
    print "no sheet in %s named Sheet1" % fname
    return None
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)

如果要使用Excel 2007,请使用openpyxlhttp://packages.python.org/openpyxl/

对于“xls”文件,可以使用xlutils包。由于Excel格式的结构,目前不可能在openpyxl中的工作簿之间复制对象:需要管理的地方到处都是依赖关系。因此,客户端代码有责任手动复制所需的所有内容。如果时间允许,我们可以尝试将一些xlutils功能移植到openpyxl。

相关问题 更多 >