import xlrd
from xlutils.copy import copy
excel_path=
work_path=
workBook=xlrd.open_workbook(excel_path)
sheet3_content = workBook.sheet_by_index(2) #根据索引获取第三个sheet
rows = sheet3_content.col_values(10)
wb_temp=xlrd.open_workbook(excel_path) #打开待复制的表
sheet3=wb_temp.sheet_by_index(2) #根据索引获取第三个sheet
wb_all = xlrd.open_workbook(work_path) # 打开待粘贴的表
new_wb =copy(wb_all) #将xlrd对象拷贝转化为xlwt对象,要用到xlutils模块
new_sheet = new_wb.get_sheet(0)#获取转化后工作簿中的第一个sheet
k=8675
for i in range(2188,3788):
print('正在复制第%s行'%i)
print('复制次数:%s'%rows[i])
Number_of_copies=rows[i]
row = sheet3.row_values(i) # 获取第i行的内容,w为一个list
try:
while Number_of_copies > 0:
for s, content in enumerate(row):
new_sheet.write(k, s, content)
k += 1
Number_of_copies -= 1
except TypeError:
for s, content in enumerate(row):
new_sheet.write(k, s, content)
k += 1
finally:
new_wb.save(work_path) # 保存工作簿
print('复制成功')
i+=1
将表格中的一行复制多行到另一个表格末尾
最新推荐文章于 2023-04-16 21:49:42 发布