1. 程式人生 > 實用技巧 >Python利用xlutils統計excel表格資料

Python利用xlutils統計excel表格資料

假設有像上這樣一個表格,裡面裝滿了各式各樣的資料,現在要利用模板對它進行統計每個銷售商的一些資料的總和。模板如下:

程式碼開始:

 1 #!usr/bin/python3
 2 # -*-coding=utf-8 -*-
 3 
 4 import xlrd
 5 import xlwt
 6 from xlutils.copy import copy
 7 
 8 xlsx = xlrd.open_workbook('template.xls') #開啟資料來源工作簿
 9 table = xlsx.sheet_by_index(0) #讀取表格索引為0的表
10 
11 all_data = [] #
此列表用於儲存我們需要的在excel中的內容 12 13 #for迴圈讀取表格 14 for n in range(1, table.nrows): #nrows代表excel表格的每一樣;從1開始代表忽略表頭,也就是從第二行開始 15 company = table.cell(n,1).value #每次迴圈都讀取n+1行第2列的內容並賦值給company 16 price = table.cell(n,3).value #每次迴圈都讀取n+1行第4列的內容並賦值給price 17 weight = table.cell(n,4).value #每次迴圈都讀取n+1行第5列的內容並賦值給weight
18 19 data = {'company':company, 'weight':weight, 'price':price} #用字典儲存內容 20 all_data.append(data) #將字典逐一追加到空列表 21 22 #新建空列表用於儲存資料 23 #儲存銷售商張三糧配的資料總重量和總價格 24 a_total_weight = [] 25 a_total_price = [] 26 #儲存銷售商李四糧食的資料總重量和總價格 27 b_total_weight = [] 28 b_total_price = [] 29 #儲存銷售商王五小麥的資料總質量和總價格
30 c_total_weight = [] 31 c_total_price = [] 32 #儲存銷售商趙六麥子專營的資料的總重量和總價格 33 d_total_weight = [] 34 d_total_price = [] 35 36 #開始迴圈列表 37 for i in all_data: 38 if i['company'] == "張三糧配": 39 a_total_weight.append(i['weight']) 40 a_total_price.append(i['weight'] * i['price']) 41 if i['company'] == "李四糧食": 42 b_total_weight.append(i['weight']) 43 b_total_price.append(i['weight'] * i['price']) 44 if i['company'] == "王五小麥": 45 c_total_weight.append(i['weight']) 46 c_total_price.append(i['weight'] * i['price']) 47 if i['company'] == "趙六麥子專營": 48 d_total_weight.append(i['weight']) 49 d_total_price.append(i['weight'] * i['price']) 50 51 #選擇模板 52 tem_excel = xlrd.open_workbook('countTemplate.xls',formatting_info=True) 53 tem_sheet = tem_excel.sheet_by_index(0) 54 55 #複製模板 56 new_excel = copy(tem_excel) 57 new_sheet.get_sheet(0) 58 59 #新增樣式 60 style = xlwt.XFStyle() 61 62 #設定樣式字型 63 font = xlwt.Font() 64 font.name = "微軟雅黑" #設定字型名稱 65 font.bold = True #設定字型加粗 66 font.height = 200 #設定字型大小,字型大小的演算法是字型大小乘以20; 67 style.font = font #將字型樣式新增到整體樣式 68 69 #設定邊框樣式 70 borders = xlwt.Borders() 71 borders.top = xlwt.Borders.THIN #設定上邊框的樣式為細線 72 borders.left = xlwt.Borders.THIN #設定左邊框的樣式為細線 73 borders.bottom = xlwt.Borders.THIN #設定下邊框的樣式為細線 74 borders.right = xlwt.Borders.THIN #設定右邊框的樣式為細線 75 style.borders = borders #將邊框樣式新增到整體樣式 76 77 #設定對齊樣式 78 alignment = xlwt.Alignment() 79 alignment.horz = xlwt.Alignment.HORZ_CENTER #設定水平對齊樣式為居中 80 alignment.vert = xlwt.Alignment.VERT_CENTER #設定垂直對齊樣式為居中 81 style.alignment = alignment #加對齊樣式新增到總樣式 82 83 #開始寫入資料 84 new_sheet.write(2,1, len(a_total_weight), style) 85 new_sheet.write(2,2, round(sum(a_total_weight), 2), style) 86 new_sheet.write(2,3, round(sum(a_total_price), 2), style) 87 new_sheet.write(3,1, len(b_total_weight), style) 88 new_sheet.write(3,2, round(sum(b_total_weight), 2), style) 89 new_sheet.write(3,3, round(sum(b_total_price), 2), style) 90 new_sheet.write(4,1, len(c_total_weight), style) 91 new_sheet.write(4,2, round(sum(c_total_weight), 2), style) 92 new_sheet.write(4,3, round(sum(c_total_price), 2), style) 93 new_sheet.write(5,1, len(d_total_weight), style) 94 new_sheet.write(5,2, round(sum(d_total_weight), 2), style) 95 new_sheet.write(5,3, round(sum(d_total_price), 2), style) 96 97 #儲存工作簿 98 new_excel.save('results.xls')

最終效果:

總結步驟:

1,先開啟要處理的資料表;

2,根據要求讀取資料表的內容並別儲存;

3,複製模板並存儲為新的工作簿;

4,向新的工作簿裡寫入提取的要求資料;

5,設定樣式;

6,儲存工作簿。