欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python2 對(duì)excel表格操作完整示例

 更新時(shí)間:2020年02月23日 10:29:37   作者:Dawn__Z  
這篇文章主要介紹了python2 對(duì)excel表格操作,結(jié)合完整實(shí)例形式分析了Python2 操作Excel表格的讀取、寫(xiě)入相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了python2 對(duì)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):
#  設(shè)置單元格樣式
  style = xlwt.XFStyle() # 初始化樣式
  font = xlwt.Font()  # 設(shè)置字體樣式
  font.name = name   # 字體名稱(chēng)'Times New Roman'
  font.bold = bold   #加粗 
  font.color_index = 4 #顏色
  font.height = height #高度
  style.font = font     #定義字體屬性
  return style
def write_excel(bomcode ): #創(chuàng)建標(biāo)準(zhǔn)件模板
  excel = xlwt.Workbook() #創(chuàng)建工作簿
  import datetime
  dateTime=datetime.datetime.now().strftime('%Y-%m-%d')
  sheet1 = excel.add_sheet(u'標(biāo)貼防錯(cuò)系統(tǒng)',cell_overwrite_ok=True)
  #生成表頭,即第一列
  sheet1.write(0,0,u'訂單編碼',set_style('Times New Roman',180))
  sheet1.write(0,1,u'當(dāng)前時(shí)間',set_style('Times New Roman',180))
  sheet1.write(0,2,u'檢測(cè)總量', set_style('Times New Roman', 180))
  sheet1.write(0,3,u'出錯(cuò)數(shù)量', set_style('Times New Roman', 180))
  sheet1.write(0, 4, u'正確數(shù)量', 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): # 添加內(nèi)容
  import datetime
  dateTime = datetime.datetime.now().strftime('%Y-%m-%d') # 查看時(shí)間
  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))
  #增加異常,文件名需按最新時(shí)間排列
  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)

運(yùn)行后將數(shù)據(jù)寫(xiě)入如下xls文件:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python操作Excel表格技巧總結(jié)》、《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • pandas dataframe添加表格框線輸出的方法

    pandas dataframe添加表格框線輸出的方法

    今天小編就為大家分享一篇pandas dataframe添加表格框線輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • python寫(xiě)一個(gè)md5解密器示例

    python寫(xiě)一個(gè)md5解密器示例

    這篇文章主要介紹了python寫(xiě)一個(gè)md5解密器示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • Python制作個(gè)性化的詞云圖實(shí)例講解

    Python制作個(gè)性化的詞云圖實(shí)例講解

    大家好,本篇文章主要講的是Python制作個(gè)性化的詞云圖實(shí)例講解,感興趣的同學(xué)趕緊來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-02-02
  • python 多線程中join()的作用

    python 多線程中join()的作用

    這篇文章主要介紹了python 多線程中join()的作用,通過(guò)代碼實(shí)踐來(lái)加深對(duì) join()的認(rèn)識(shí),幫助大家更好的理解和學(xué)習(xí)python 多線程,感興趣的朋友可以了解下
    2020-10-10
  • python中如何使用insert函數(shù)

    python中如何使用insert函數(shù)

    這篇文章主要介紹了python中如何使用insert函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加教程

    Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加教程

    這篇文章主要為大家介紹了Python區(qū)塊鏈范圍結(jié)論及Genesis Block的添加,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • PyQt5 加載圖片和文本文件的實(shí)例

    PyQt5 加載圖片和文本文件的實(shí)例

    今天小編就為大家分享一篇PyQt5 加載圖片和文本文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python爬取科目四考試題庫(kù)的方法實(shí)現(xiàn)

    Python爬取科目四考試題庫(kù)的方法實(shí)現(xiàn)

    這篇文章主要介紹了Python爬取科目四考試題庫(kù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例

    Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例

    今天小編就為大家分享一篇Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • python urllib.request模塊的使用詳解

    python urllib.request模塊的使用詳解

    這篇文章主要介紹了python urllib.request模塊的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03

最新評(píng)論