用python编写一个合并excel表的程序
时间: 2023-03-09 08:41:48 浏览: 102
我可以为你提供一个简易的python程序来合并excel表:# 导入xlrd和xlwt库
import xlrd
import xlwt
# 创建xlwt对象
workbook = xlwt.Workbook()
# 创建sheet对象
sheet = workbook.add_sheet('Sheet1')
# 定义要合并的excel表格
table_one = '表1.xls'
table_two = '表2.xls'
# 打开excel表
data_one = xlrd.open_workbook(table_one)
data_two = xlrd.open_workbook(table_two)
# 获取要合并的sheet页
table_one = data_one.sheet_by_index(0)
table_two = data_two.sheet_by_index(0)
# 获取表格的行数和列数
nrows_one = table_one.nrows
ncols_one = table_one.ncols
nrows_two = table_two.nrows
ncols_two = table_two.ncols
# 将表格一的数据写入excel
for i in range(0, nrows_one):
for j in range(0, ncols_one):
sheet.write(i, j, table_one.cell_value(i, j))
# 将表格二的数据写入excel
for m in range(0, nrows_two):
for n in range(0, ncols_two):
sheet.write(m+nrows_one, n, table_two.cell_value(m, n))
# 保存excel文件
workbook.save('合并后的表.xls')
阅读全文