惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
D
Docker
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 叶小钗
I
InfoQ
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
B
Blog
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium

Mereith's Blog

记一次代理转发问题的排查与解决 PVE DataCenter Manager 端口号跳转错误修复 k8s + jenkins + gitlab 触发器 【转载】没有编程生产力这样的东西 【后续】居然被 ddos 了 居然被 ddos 了 Tagger - 让版本标签管理更简单 openclash 开启后端口转发失效 nextjs 启动时执行代码 买了太多VPS不知道干什么,干脆做个网站 juhost Level-1 测评 VMISS CN - Hong Kong VMISS JP - Tokyo - BGP 测评 VMISS CN - Hong Kong - BGP HomeLab 的终点是 最近在做的事情 因为选择艰难症,自己写了一套开源博客系统 手撸一个nodejs分布式爬虫,还要可视化 HomeLab 分享 我的个人工作流——开源项目推荐 动手写一个超简单的编译器 safari 插件开发 Safari Extensions Preferences 按钮无响应 设置移动端软键盘回车按钮文案 nginx proxy manager 非标准端口反代 Host 不对 监控 k8s ingress 自动添加域名 DNS 解析 使用 ingress 注解给 traefik ingress 添加中间件 使用流水线功能为文章添加固定结尾 为 VanBlog 添加一个小挂件 基于React/umi/egg自建博客系统 如何正确停止 NodeJS 子进程
【python实用系列】python从不同的excel收集数据并汇总
wanglu@mereith.com (mereith) · 2022-07-21 · via Mereith's Blog

请注意,本文编写于 2259 天前,最后修改于 1416 天前,其中某些信息可能已经过时。

最近帮同学处理一些数据,目的是分别从不同的一些excel文件中收集数据,按照格式汇总到生成的excel中. 保存下来,避免以后忘了,主要用到的就是xlrd和xlwt两个模块。

参考:

python

模块

python模块

代码

import xlwt,xlrd import os # 初始化工作表 workbook = xlwt.Workbook(encoding='utf-8') sheet2014 = workbook.add_sheet('2014') sheet2015 = workbook.add_sheet('2015') sheet2016 = workbook.add_sheet('2016') sheet2017 = workbook.add_sheet('2017') sheet2018 = workbook.add_sheet('2018') sheets = [sheet2014,sheet2015,sheet2016,sheet2017,sheet2018] #style 对齐设置 alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_CENTER alignment.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() style.alignment = alignment # 初始化第一列 def init(name): name.write(0,0,'2014',style) name.write(0,1,'盈利能力:净资产收益率',style) name.write(0,2,'总资产净利率',style) name.write(0,3,'投入资本回报率',style) name.write(0,4,'销售净利率',style) name.write(0,5,'销售毛利率',style) name.write(0,6,'各项指标在营业总收入中占比:营业利润/营业总收入',style) name.write(0,7,'营业总成本/营业总收入',style) name.write(0,8,'销售费用/营业总收入',style) name.write(0,9,'管理费用/营业总收入',style) name.write(0,10,'财务费用/营业总收入',style) for sheet_item in sheets: init(sheet_item) # 写入需要的数据的函数 def writedata(tar,src,name,i): i = i+1 tar.write(i,0,name,style) tar.write(i,1,src[3],style) tar.write(i,2,src[13],style) tar.write(i,3,src[15],style) tar.write(i,4,src[17],style) tar.write(i,5,src[18],style) tar.write(i,6,src[22],style) tar.write(i,7,src[25],style) tar.write(i,8,src[26],style) tar.write(i,9,src[27],style) tar.write(i,10,src[28],style) #按顺序加载列表 idd = 0 lists = xlrd.open_workbook(r'C:\Users\pve_win10_1\Desktop\xialinya\raw.xls').sheet_by_index(0).col_values(0)[16:26] print('读取文件',':',lists) for item in lists: # 获取公司名称 name = item # 获取工作表 data = xlrd.open_workbook(item+'.xlsx').sheet_by_index(0) # 查找年份在哪里列 row1 = data.row_values(0) for i in range(len(row1)): index = row1[i].find('2014') if not index: # 如果这一项有2014,那么2015等就依次减1 # 依次得到各个年份的数据哦 data2014 = data.col_values(i) data2015 = data.col_values(i-1) data2016 = data.col_values(i-2) data2017 = data.col_values(i-3) data2018 = data.col_values(i-4) datas = [data2014,data2015,data2016,data2017,data2018] # 写入这个公司的数据 for i in range(5): writedata(sheets[i],datas[i],name,idd) idd = idd + 1 # 设置单元格高度 for each in sheets: for i in range(12): each.col(i).width = 5000 for i in range(15): each.row(i).height_mismatch = True each.row(i).height = 300 # 保存文件 workbook.save(r'C:\Users\pve_win10_1\Desktop\export\数据收集.xls') print('over!')