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

代码开始:

 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. django基本内容

    1,流程 1.1 了解web程序工作流程 1.2 django生命周期 2,django介绍 ​ 目的:了解Django框架的作用和特点 ​ 作用: 简便.快速的开发数据库驱动的网站 django的优 ...

  2. O - Matching 题解(状压dp)

    题目链接 题目大意 给你一个方形矩阵mp,边长为n(n<=21) 有n个男生和女生,如果\(mp[i][j]=1\) 代表第i个男生可以和第j个女生配对 问有多少种两两配对的方式,使得所有男生和 ...

  3. 03-Python里字符串的常用操作方法二

    1.lstrip():删除左侧空白字符 实例: my_str = ' hello world and my and test and python ' # 原始字符串 print(my_str) # ...

  4. PTA天梯赛校内模拟

    最长对称子串 || 区间dp || 马拉车 dp[i][j]表示区间[i, j]是否为回文串,若是则为1,不是则为0. 边界条件: 1. 区间长度为1,dp为1.(奇数个字符递推的起始情况) 2. 区 ...

  5. ActiveMQ Cannot send, channel has already failed: tcp:127.0.0.1:8161

    仅针对如下错误内容: Cannot send, channel has already failed: tcp://127.0.0.1:8161 一种尝试解决,修改连接端口为 61616: tcp:/ ...

  6. Mac MySQL 8.0 (免安装版) 主从集群搭建

    一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...

  7. 泓格WINPAC主机与第三方模块rs 485 modbus rtu通信测试

    开发语言:C# 开发环境:VS2008(支持WINCE开发的最后一个版本) 运行环境:Windows CE 5.0~7.0 项目说明:多台涨格winpac系列的主机,原来使用泓格SDK开发的程序,采集 ...

  8. JDK8日期类入门

    关于jdk8的时间类的用法,网上有很多教程教你如何用,比如: System.out.println(LocalDateTime.now()); 可以获取当前的时间, 2020-12-06T18:02: ...

  9. CAS学习过程中的一些记录

    1 inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { 2 int ...

  10. PyQt(Python+Qt)入门:设计师中部件toolTip、statusTip、whatsThis的属性

    在Qt Designer中定义的部件,都有toolTip.statusTip.whatsThis,这些属性都是辅助提示的信息. toolTip toolTip属性设置部件的toolTip提示信息,to ...