相思资源网 Design By www.200059.com
本文实例讲述了python2 对excel表格操作。分享给大家供大家参考,具体如下:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 2 15:40:35 2017
@author: 260207
"""
from xlutils.copy import copy
import xlrd
import xlwt
def set_style(name = 'Times New Roman',height = 6,bold=False):
# 设置单元格样式
style = xlwt.XFStyle() # 初始化样式
font = xlwt.Font() # 设置字体样式
font.name = name # 字体名称'Times New Roman'
font.bold = bold #加粗
font.color_index = 4 #颜色
font.height = height #高度
style.font = font #定义字体属性
return style
def write_excel(bomcode ): #创建标准件模板
excel = xlwt.Workbook() #创建工作簿
import datetime
dateTime=datetime.datetime.now().strftime('%Y-%m-%d')
sheet1 = excel.add_sheet(u'标贴防错系统',cell_overwrite_ok=True)
#生成表头,即第一列
sheet1.write(0,0,u'订单编码',set_style('Times New Roman',180))
sheet1.write(0,1,u'当前时间',set_style('Times New Roman',180))
sheet1.write(0,2,u'检测总量', set_style('Times New Roman', 180))
sheet1.write(0,3,u'出错数量', set_style('Times New Roman', 180))
sheet1.write(0, 4, u'正确数量', set_style('Times New Roman', 180))
sheet1.write(0,5,u'合格率', set_style('Times New Roman', 180))
excel.save(file_dir+str(dateTime)+'.xls') #保存文件
#
def seefile(file_dir):
import os
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.xls':
L.append(os.path.join(root, file))
return L
def add_excel(passflag,row,error,true): # 添加内容
import datetime
dateTime = datetime.datetime.now().strftime('%Y-%m-%d') # 查看时间
nowTime = datetime.datetime.now().strftime('%H:%M:%S')
workbook = xlrd.open_workbook(file_dir+str(dateTime)+'.xls','r+')
new_excel = copy(workbook)
ws = new_excel.get_sheet(0) # 索引到表格
ws.write(row, 0, str(bomcode), set_style('Times New Roman', 180))
ws.write(row, 1, str(nowTime), set_style('Times New Roman', 180))
if passflag == 1:
ws.write(row, 3, error+1, set_style('Times New Roman', 180))
ws.write(row, 4, true, set_style('Times New Roman', 180))
else:
ws.write(row, 4, true+1, set_style('Times New Roman', 180))
ws.write(row, 3, error, set_style('Times New Roman', 180))
ws.write(row, 2, error+true+1, set_style('Times New Roman', 180))
new_excel.save(file_dir+str(dateTime)+'.xls')
def pass_rate(row):
import datetime
dateTime = datetime.datetime.now().strftime('%Y-%m-%d')
workbook112 = xlrd.open_workbook(file_dir+str(dateTime)+'.xls','r+')
all_excel = copy(workbook112)
ws1 = all_excel.get_sheet(0)
workbook_position = workbook112.sheet_by_index(0)
all_value=workbook_position.cell(row,2).value
true_value=workbook_position.cell(row,4).value
ws1.write(row, 5, round(float(true_value)/(all_value),2), set_style('Times New Roman', 180))
all_excel.save(file_dir+str(dateTime)+'.xls')
def data_analysis(bomcode):
import datetime
dateTime = datetime.datetime.now().strftime('%Y-%m-%d')
filename = seefile(file_dir)
filename = list(reversed(filename))
#增加异常,文件名需按最新时间排列
if (filename ==[]) or (filename is None) or (dateTime!=(filename[0])[-14:-4]) :
write_excel(bomcode)
workbook = xlrd.open_workbook(file_dir+str(dateTime)+'.xls','r+')
workbook_position = workbook.sheet_by_index(0)
cols_0 = workbook_position.col_values(0)
if bomcode in cols_0:
row_error = cols_0.index(bomcode)
error=workbook_position.cell(row_error,3).value
true=workbook_position.cell(row_error,4).value
row = row_error
else :
error = 0
true = 0
row = len(cols_0)
add_excel(passflag,row,error,true)
pass_rate(row)
if __name__ == '__main__':
file_dir ='C:/Users/Administrator/Desktop/Data_analysis/'
bomcode='21122'
passflag =0
data_analysis(bomcode)
# add_excel(bomcode,passflag,row)
运行后将数据写入如下xls文件:
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python操作Excel表格技巧总结》、《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无python2 对excel表格操作完整示例的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
