2020.3.5 学习把一个excel的表导入模板当中


import xlrd
from xlutils.copy import copy
xlsx=xlrd.open_workbook("d:/3.5数据表.xlsx")
table=xlsx.sheet_by_index(0)
all_data=[]

for n in range(1,table.nrows):
company=table.cell(n,1).value
price=table.cell(n,3).value
weight=table.cell(n,4).value
data=(company,price,weight)
all_data.append(data)
a_weight=[]
a_total_price=[]
b_weight = []
b_total_price = []
c_weight = []
c_total_price = []
d_weight = []
d_total_price = []
for i in all_data:
if i[0]=="张三小米":
a_weight.append(i[1])
a_total_price.append(i[1]*i[2])
if i[0] == "李四粮食":
b_weight.append(i[1])
b_total_price.append(i[2] * i[2])
if i[0] == "王五小麦":
c_weight.append(i[1])
c_total_price.append(i[1] * i[2])
if i[0] == "赵六玉米":
d_weight.append(i[1])
d_total_price.append(i[1] * i[2])
tem_excel=xlrd.open_workbook("d:/3.5数据表模板.xlsx")
new_excel=copy(tem_excel)
new_sheet=new_excel.get_sheet(0)


new_sheet.write(2,1,len(a_weight))
new_sheet.write(2,2,sum(a_weight))
new_sheet.write(2,3,sum(a_total_price))

new_sheet.write(3,1,len(b_weight))
new_sheet.write(3,2,sum(b_weight))
new_sheet.write(3,3,sum(b_total_price))

new_sheet.write(4,1,len(c_weight))
new_sheet.write(4,2,sum(c_weight))
new_sheet.write(4,3,sum(c_total_price))

new_sheet.write(5,1,len(d_weight))
new_sheet.write(5,2,sum(d_weight))
new_sheet.write(5,3,sum(d_total_price))

new_excel.save("d:/xinjian11.xlsx")



posted @ 2020-03-05 15:32  fakerbenren  阅读(231)  评论(0)    收藏  举报