Python 通过 xlwings 读取 Excel 数据

去年底公司让我做设备管理,多次委婉拒绝,最终还是做了。其实我比较喜欢技术。做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Excel中填写数据,感觉特麻烦了,就写了这个自动化操作Excel的程序,以前要半天的工作量,用Python不到10s。程序比较简单,直接上程序。

# -*- coding: utf-8 -*-

import xlwings as xw
import pandas as pd
from datetime import datetime # 统计时间, 只有时间要改
START_TIME = '2018-07-01 00:00:00'
END_TIME = '2018-07-31 23:59:00' START_ROW = 2 # 处理Excel文件开始行
END_ROW = 200 # 处理Excel结束行 # 天数 * 每天工作时间 * 分钟
WORK_TIME = 30 * 22 * 60 # 关键设备清单
key_machine = ['609', '610', '621', '622', '623', '624',
'627', '628', '636', '638', '667', '670', '675', '689']
persons = ['张三', '李四', '王五'] app = xw.App(visible=True, add_book=False)
wb_source = app.books.open('downTimeData.xls') # 打开Excel文件 downTimeData.xls
sheet = wb_source.sheets[0] # 选择第0个表单 # 需每月修改时间
start_datetime = datetime.strptime(START_TIME, '%Y-%m-%d %H:%M:%S') # 把开始统计时间转换为DateTime
end_datetime = datetime.strptime(END_TIME, '%Y-%m-%d %H:%M:%S') # 把结束统计时间转换为DateTime result = [] for row in range(START_ROW, END_ROW):
row_content = []
row_str = str(row) time_str = sheet.range('C' + row_str).value.strip()
create_datetime = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
if start_datetime <= create_datetime <= end_datetime: machine = sheet.range('A' + row_str).value
machine_number = machine[-4:-1]
if machine_number in key_machine: if sheet.range('G' + row_str).value.strip() in persons:
row_content.append(machine_number) row_content.append(create_datetime) response_time_str = sheet.range('D' + row_str).value
complete_time_str = sheet.range('E' + row_str).value
row_content.append(response_time_str + complete_time_str) solution_str = sheet.range('H' + row_str).value.strip()
row_content.append(solution_str) comments = sheet.range('I' + row_str).value.strip()
row_content.append(comments) result.append(row_content) # count the times and downtime on the same machine and put it in dictionary
# 统计每台设备的停机次数
dict_result = {}
for _, [name, _, downtime, _, _] in enumerate(result):
if name in dict_result:
dict_result[name] = (dict_result[name][0] + 1,
dict_result[name][1] + downtime)
else:
dict_result[name] = (1, downtime) # fill the result and write it on excel
target_name = START_TIME[5:7]
wb_target = app.books.open('analysis2018.xlsx') # 打开Excel文件,把结果写入 index = 3
for key in key_machine:
if key not in dict_result:
wb_target.sheets[target_name].range('B' + str(index)).value = 0
wb_target.sheets[target_name].range('C' + str(index)).value = 0
wb_target.sheets[target_name].range('D' + str(index)).value = WORK_TIME
wb_target.sheets[target_name].range('E' + str(index)).value = 0
wb_target.sheets[target_name].range('F' + str(index)).value = 0
else:
wb_target.sheets[target_name].range(
'B' + str(index)).value = dict_result[key][0]
wb_target.sheets[target_name].range(
'C' + str(index)).value = dict_result[key][1]
wb_target.sheets[target_name].range(
'D' + str(index)).value = (WORK_TIME - dict_result[key][1]) / dict_result[key][0]
wb_target.sheets[target_name].range(
'E' + str(index)).value = dict_result[key][1] / dict_result[key][0]
wb_target.sheets[target_name].range(
'F' + str(index)).value = dict_result[key][1] / WORK_TIME
index += 1 # write the comment and solution on excel
result.sort() # 故障信息排序,用于最后输出
df = pd.DataFrame(result, columns=['编号',
'故障时间',
'停机时间', '解决方案', '备注']) wb_target.sheets[target_name].range('H2').value = df
wb_target.sheets[target_name].autofit('c')

利用 Python 插件 xlwings 读写 Excel的更多相关文章

  1. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  2. 【转发】Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  3. Python用openpyxl读写Excel

    openpyxl是一个用于读写Excel 2010 xlsx文件的python库.openpyxl官方文档:https://openpyxl.readthedocs.io/en/stable/ 一.安 ...

  4. python xlrd,xlwt 读写excel文件

    python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...

  5. 用插件NPOI读写excel

    1.用插件NPOIusing NPOI.SS.UserModel;using NPOI.XSSF.UserModel;using NPOI.HSSF.UserModel; public class E ...

  6. Python用Pandas读写Excel

    Pandas是python的一个数据分析包,纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速便捷地处理数据的函数和方法. Pandas官方文档 ...

  7. python 单元测试_读写Excel及配置文件(八)

    一.安装openpyxl模块 openpyxl模块:是用于解决Excel(WPS等均可使用)中扩展名为xlsx/xlsm/xltx/xltm的文件读写的第三方库.xls文件要使用xlwt .wlrd两 ...

  8. 利用NPOI开源的读写Excel、WORD等微软OLE2组件读写execl,控制样式或单元格

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. Python 使用 xlwings 往 excel 中写入一行数据的两种方法

    该方法跟上一篇写入一列的方法相反,代码如下: # -*- coding:utf-8 -*- import xlwings as xw list1 = [1,2,3,4,5] list2 = [[1], ...

随机推荐

  1. ubuntu 启用root用户方法

    1.按下ctrl + alt + T,输入 sudo passwd root设置root的密码,如下图所示: 2.使用su root来测试是否可以进入root用户,如果出现#说明已经设置root用户的 ...

  2. Oracle GoldenGate 详解

    一.Oracle GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与目标数据库的数据 ...

  3. MyCAT详解

    一.MyCAT概述 MyCAT是一款由阿里Cobar演变而来的用于支持数据库读写分离.分片的分布式中间件.MyCAT可不但支持Oracle.MSSQL.MYSQL.PG.DB2关系型数据库,同时也支持 ...

  4. MVC中重定向几种方法

    //1.Response.Redirect using System; using System.Collections.Generic; using System.Linq; using Syste ...

  5. LINUX中块设备文件和字符设备文件的本质区别

    在LINUX系统文件类型分类的文章中我们提到了 块设备 和 字符设备文件,那么什么是块设备 字符设备文件呢?他们之间有什么本质上的区别呢? 设备文件分为 Block(块) Device Driver ...

  6. css笔记 2

    定义一个类选择器.center {text-align: center} h1 有 center 类.这意味将遵守 ".center" 选择器中的规则.<h1 class=& ...

  7. 全国大学生数据挖掘邀请赛中的NDCG

    转:http://www.zhizhihu.com/html/y2011/2794.html 评价标准 性能良好的评分模型,应该能够给予那些引起msg或click的候选会员更高的评分(排序靠前),从而 ...

  8. background-color和background-image相关细节

    1.background-color 是以border-box作为他的左上角来定位的 2.background-image 默认是以padding-box作为他的左上角来定位的 3.backgroun ...

  9. codeforces 848B Rooter's Song

    题目链接 正解:排序+模拟. 我们注意到两个点碰撞的必要条件,$pi+tj=pj+ti$,移项以后发现就是$pi-ti=pj-tj$,那么我们可以把$p-t$相同的点分为同一组. 然后我们还可以发现一 ...

  10. webpack-cli解决办法

    错误信息:Error Cannot find module 'webpack-cli 出现这个错误的原因是因为只全局安装了webpack,没有安装对应的webpack-cli.再打包时是需要webpa ...