假设有像上这样一个表格,里面装满了各式各样的数据,现在要利用模板对它进行统计每个销售商的一些数据的总和。模板如下:

代码开始:

 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,保存工作簿。

Python利用xlutils统计excel表格数据的更多相关文章

  1. Visual Studio 2010利用libxl读写excel表格数据

    C++读写数据,一般通过txt文件,但是随着数据量的增大,采集数据时运用excel表格的优势得以逐步体现.本文主要介绍一下运用第三方库libxl,对excel表格数据进行读写.分为三个部分,第一部分是 ...

  2. Python利用pandas处理Excel数据的应用

    Python利用pandas处理Excel数据的应用   最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...

  3. 利用 pandas库读取excel表格数据

    利用 pandas库读取excel表格数据 初入IT行业,愿与大家一起学习,共同进步,有问题请指出!! 还在为数据读取而头疼呢,请看下方简洁介绍: 数据来源为国家统计局网站下载: 具体方法 代码: i ...

  4. 利用PHPExcel读取Excel的数据和导出数据到Excel

    PHPExcel是一个PHP类库,用来帮助我们简单.高效实现从Excel读取Excel的数据和导出数据到Excel.也是我们日常开发中,经常会遇到的使用场景.比如有个客户信息表,要批量导出发给同事,我 ...

  5. 利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图、折线图、饼图

    利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图.折线图.饼图 数据: 折线图代码: import  pandas  as pdimport  matplotlib. ...

  6. Python将多个excel表格合并为一个表格

    Python将多个excel表格合并为一个表格 生活中经常会碰到多个excel表格汇总成一个表格的情况,比如你发放了一份表格让班级所有同学填写,而你负责将大家的结果合并成一个.诸如此类的问题有很多.除 ...

  7. jxl读取Excel表格数据

    调用jxl包实现Excel表格数据的读取,代码如下: import java.io.File; import java.io.IOException; import java.util.ArrayLi ...

  8. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表

    调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...

  9. JXL读取写入excel表格数据

    问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...

随机推荐

  1. 团队 Gitee 实战训练

    这个课程属于 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs.com/campus/fzzcxy ...

  2. 新手上路A4——多JDK环境变量的配置

    目录 配置单个JDK的方法 配置2+JDK的方法 方法 补充 检查JDK版本是否切换成功 前面讲了如何选择Java版本. 以及JDK8和JDK11的下载安装配置 有想法的人就开始发动他们优秀的小脑袋瓜 ...

  3. 转:解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别

    解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别 标准Http协议支持六种请求方法,即: 1.GET 2.POST 3.PUT 4.Delete 5.HEAD ...

  4. 第12.3节 Python math模块导览

    math 模块提供对浮点数学的底层C库函数的访问,常用的成员包括: math.ceil(x):返回 x 的上限,即大于或者等于 x 的最小整数 math.floor(x):返回 x 的向下取整,小于或 ...

  5. Hello!OA!Hello!工作流!寻找OA和工作流的旅途记录

    最近新到了一家公司,这家公司做的人力资源管理,需要一个OA系统,所以就让我做一个选型,经过我2周时间的筛选,试用,沟通,测试,最终确定了几款,这个艰辛的路程,在这里记录一下~ 寻找OA的路程----- ...

  6. justify-content属性详解

    justify-content 定义了flexbox flexbox内的元素在主轴的方向上的对齐方式. 它可以设置以下几种对齐方式: 靠近一方 justify-content:center: /*fl ...

  7. python web的一些常见技术面试笔试题

    1. 三次握手四次挥手   tcp建立连接的过程是三次挥手,断开连接是4次挥手. 三次握手:建立连接时 a. 客户端发送syn=1 seq=k给服务器 b. 服务器接收到之后知道有客户端想建立连接, ...

  8. 【题单】最近遇见的 SHIT DP题 三连

    Hint: 本题单适合用于自虐和消磨时间. CF-Gym101620E https://codeforces.com/gym/101620 ARC109F https://atcoder.jp/con ...

  9. AH/HNOI 2017 礼物

    题目链接 描述 两个序列 \(x, y\),可以将一个序列每个值同时加非负整数 \(c\),其中一个序列可以循环移位,要求最小化: \[\sum_{i = 1}^{n}(x_i - y_i) ^ 2 ...

  10. mac中nvm的安装和使用

    nvm 是 Mac 下的 node 管理工具,如果是管理 Windows 下的 node,可以使用 nvmw 或 nvm-windows . 一.若电脑中已安装node,需先卸载.参考学习的文档:ht ...