最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)的Excel文件讀寫類

 更新時間:2015年07月30日 14:46:21   作者:defias  
這篇文章主要介紹了Python實(shí)現(xiàn)的Excel文件讀寫類,涉及Python針對Excel常見的讀寫、打印等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的Excel文件讀寫類。分享給大家供大家參考。具體如下:

#coding=utf-8
#######################################################
#filename:ExcelRW.py
#author:defias
#date:2015-4-27
#function:read or write excel file
#######################################################
import xlrd
import xlwt
import xlutils.copy 
import os.path
class XlsEngine():
 """
 The XlsEngine is a class for excel operation
 Usage: 
  xlseng = XlsEngine('filePath') 
 """
 def __init__(self,xlsname):
  """
  define class variable
  """
  self.xls_name = xlsname #file name
  self.xlrd_object = None #workbook object
  self.isopentrue = False #file open flag
 def open(self):
  """
  open a xls file
  Usage:
   xlseng.open()
  """
  try:
   self.xlrd_object = xlrd.open_workbook(self.xls_name)
   self.isopentrue = True
   print('[%s,%s].'%(self.isopentrue,self.xlrd_object))
  except:
   self.isopentrue = False
   self.xlrd_object = None
   print('open %s failed.'%self.xls_name)
 def info(self):
  """
  show xls file information
  Usage:
   xlseng.info()  
  """
  if self.isopentrue == True:
   for sheetname in self.xlrd_object.sheet_names():
    worksheet = self.xlrd_object.sheet_by_name(sheetname)
    print('%s:(%d row,%d col).'%(sheetname,worksheet.nrows,worksheet.ncols))
  else:
   print('file %s is not open.'%self.xls_name)
 def readcell(self,sheetname='sheet1',rown=0,coln=0):
  """
  read file's a cell content
  Usage:
   xlseng.readcell('sheetname',rown,coln)
  """
  try:
   if self.isopentrue == True:
    worksheets = self.xlrd_object.sheet_names()
    if sheetname not in worksheets:
     print('%s is not exit.'%sheetname)
     return False
    worksheet = self.xlrd_object.sheet_by_name(sheetname)
    cell = worksheet.cell_value(rown,coln)
    print('[file:%s,sheet:%s,row:%s,col:%s]:%s.'%(self.xls_name,sheetname,rown,coln,cell))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('readcell is false! please check sheetn rown and coln is right.')
 def readrow(self,sheetname='sheet1',rown=0):
  """
  read file's a row content
  Usage:
   xlseng.readrow('sheetname',rown)
  """
  try:
   if self.isopentrue == True:
    worksheets = self.xlrd_object.sheet_names()
    if sheetname not in worksheets:
     print('%s is not exit.'%sheetname)
     return False    
    worksheet = self.xlrd_object.sheet_by_name(sheetname)
    row = worksheet.row_values(rown)
    print('[file:%s,sheet:%s,row:%s]:%s.'%(self.xls_name,sheetname,rown,row))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('readrow is false! please check sheetn rown is right.')
 def readcol(self,sheetname='sheet1',coln=0):
  """
  read file's a col content
  Usage:
   xlseng.readcol('sheetname',coln)
  """
  try:
   if self.isopentrue == True:
    worksheets = self.xlrd_object.sheet_names()
    if sheetname not in worksheets:
     print('%s is not exit.'%sheetname)
     return False
    worksheet = self.xlrd_object.sheet_by_name(sheetname)
    col = worksheet.col_values(coln)
    print('[file:%s,sheet:%s,col:%s]:%s.'%(self.xls_name,sheetname,coln,col))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('readcol is false! please check sheetn coln is right.')
 def writecell(self,value='',sheetn=0,rown=0,coln=0):
  """
  write a cell to file,other cell is not change
  Usage:
    xlseng.writecell('str',sheetn,rown,coln)
  """
  try:
   if self.isopentrue == True:
    xlrd_objectc = xlutils.copy.copy(self.xlrd_object)
    worksheet = xlrd_objectc.get_sheet(sheetn)
    worksheet.write(rown,coln,value)
    xlrd_objectc.save(self.xls_name)
    print('writecell value:%s to [sheet:%s,row:%s,col:%s] is ture.'%(value,sheetn,rown,coln))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('writecell is false! please check.')
 def writerow(self,values='',sheetn=0,rown=0,coln=0):
  """
  write a row to file,other row and cell is not change
  Usage:
   xlseng.writerow('str1,str2,str3...strn',sheetn,rown.coln)
  """
  try:
   if self.isopentrue == True:
    xlrd_objectc = xlutils.copy.copy(self.xlrd_object)
    worksheet = xlrd_objectc.get_sheet(sheetn)
    values = values.split(',')
    for value in values:
     worksheet.write(rown,coln,value)
     coln += 1
    xlrd_objectc.save(self.xls_name)
    print('writerow values:%s to [sheet:%s,row:%s,col:%s] is ture.'%(values,sheetn,rown,coln))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('writerow is false! please check.')
 def writecol(self,values='',sheetn=0,rown=0,coln=0):
  """
  write a col to file,other col and cell is not change
  Usage:
   xlseng.writecol('str1,str2,str3...',sheetn,rown.coln)
  """
  try:
   if self.isopentrue == True:
    xlrd_objectc = xlutils.copy.copy(self.xlrd_object)
    worksheet = xlrd_objectc.get_sheet(sheetn)
    values = values.split(',')
    for value in values:
     worksheet.write(rown,coln,value)
     rown += 1
    xlrd_objectc.save(self.xls_name)
    print('writecol values:%s to [sheet:%s,row:%s,col:%s] is ture.'%(values,sheetn,rown,coln))
   else:
    print('file %s is not open.'%self.xls_name)
  except:
   print('writecol is false! please check.')
 def filecreate(self,sheetnames='sheet1'):
  """
  create a empty xlsfile
  Usage:
   filecreate('sheetname1,sheetname2...')
  """
  try:
   if os.path.isfile(self.xls_name):
    print('%s is exit.'%self.xls_name)
    return False
   workbook = xlwt.Workbook()
   sheetnames = sheetnames.split(',')
   for sheetname in sheetnames:
    workbook.add_sheet(sheetname,cell_overwrite_ok=True)
   workbook.save(self.xls_name)
   print('%s is created.'%self.xls_name)
  except:
   print('filerator is false! please check.')
 def addsheet(self,sheetnames='sheet1'):
  """
  add sheets to a exit xlsfile
  Usage:
   addsheet('sheetname1,sheetname2...')
  """
  try:
   if self.isopentrue == True:
    worksheets = self.xlrd_object.sheet_names()
    xlrd_objectc = xlutils.copy.copy(self.xlrd_object)
    sheetnames = sheetnames.split(',')
    for sheetname in sheetnames:
     if sheetname in worksheets:
      print('%s is exit.'%sheetname)
      return False
    for sheetname in sheetnames:
     xlrd_objectc.add_sheet(sheetname,cell_overwrite_ok=True)
    xlrd_objectc.save(self.xls_name)
    print('addsheet is ture.')
   else:
    print("file %s is not open \n"%self.xls_name)
  except:
   print('addsheet is false! please check.')
"""
  def chgsheet(self,sheetn,values):
  def clear(self):
""" 
if __name__ == '__main__': 
 #初始化對象
 xlseng = XlsEngine('E:\\Code\\Python\\test2.xls')
 #新建文件,可以指定要新建的sheet頁面名稱,默認(rèn)值新建sheet1
 #print("\nxlseng.filecreate():")
 #xlseng.filecreate('newesheet1,newesheet2,newesheet3')
 #打開文件
 print("xlseng.open():")
 xlseng.open()
 #添加sheet頁
 print("\nxlseng.addsheet():")
 xlseng.addsheet('addsheet1,addsheet2,addsheet3')
 #輸出文件信息
 print("\nxlseng.info():")
 xlseng.info()
 #讀取sheet1頁第3行第3列單元格數(shù)據(jù)(默認(rèn)讀取sheet1頁第1行第1列單元格數(shù)據(jù))
 print("\nxlseng.readcell():")
 xlseng.readcell('sheet1',2,2)
 #讀取sheet1頁第2行的數(shù)據(jù)(默認(rèn)讀取sheet1頁第1行的數(shù)據(jù))
 print("\nxlseng.readrow():")
 xlseng.readrow('sheet1',1)
 #讀取sheet1頁第3列的數(shù)據(jù)(默認(rèn)讀取sheet1頁第1列的數(shù)據(jù))
 print("\nxlseng.readcol():")
 xlseng.readcol('sheet1',2)
 #向第一個sheet頁的第2行第4列寫字符串?dāng)?shù)據(jù)‘I am writecell writed'(默認(rèn)向第一個sheet頁的第1行第1列寫空字符串)
 print("\nxlseng.writecell():")
 xlseng.writecell('I am writecell writed',0,1,3)
 #向第一個sheet頁寫一行數(shù)據(jù),各列的值為‘rowstr1,rowstr2,rowstr3',從第3行第4列開始寫入(默認(rèn)向第一個sheet頁寫一行數(shù)據(jù),值為‘',從第1行第1列開始寫入)
 print("\nxlseng.writerow():")
 xlseng.writerow('rowstr1,rowstr2,rowstr3',0,2,3)
 #向第一個sheet頁寫一列數(shù)據(jù),各行的值為‘colstr1,colstr2,colstr3,colstr4',從第4行第4列開始寫入(默認(rèn)向第一個sheet頁寫一列數(shù)據(jù),值為‘',從第1行第1列開始寫入)
 print("\nxlseng.writecol():")
 xlseng.writecol('colstr1,colstr2,colstr3,colstr4',0,3,3)

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

相關(guān)文章

  • 用基于python的appium爬取b站直播消費(fèi)記錄

    用基于python的appium爬取b站直播消費(fèi)記錄

    因工作需要,需要爬取相關(guān)數(shù)據(jù),之前是爬取網(wǎng)頁數(shù)據(jù),可以用python的requests和Selenium進(jìn)行爬取。但b站的直播消費(fèi)數(shù)據(jù)網(wǎng)頁版不能顯示,只能在手機(jī)上看到,所以就有了這篇文章。需要的朋友可以參考下
    2021-04-04
  • Python中threading模塊的Lock和RLock區(qū)別詳解

    Python中threading模塊的Lock和RLock區(qū)別詳解

    這篇文章主要介紹了Python中threading模塊的Lock和RLock區(qū)別詳解,Lock鎖是Python的原始鎖,在鎖定時不屬于任何一個線程,在調(diào)用了 lock.acquire() 方法后,進(jìn)入鎖定狀態(tài),lock.release()方法可以解鎖,底層是通過一個函數(shù)來實(shí)現(xiàn)的,需要的朋友可以參考下
    2023-09-09
  • 使用Python對Csv文件操作實(shí)例代碼

    使用Python對Csv文件操作實(shí)例代碼

    這篇文章主要介紹了使用Python對Csv文件操作實(shí)例代碼,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-05-05
  • Python腳本文件打包成可執(zhí)行文件的方法

    Python腳本文件打包成可執(zhí)行文件的方法

    這篇文章主要介紹了Python腳本文件打包成可執(zhí)行文件的方法,本主要講解了Python2.X版本的打包方法,對Python3.X的打包也有簡單介紹,需要的朋友可以參考下
    2015-06-06
  • Pytorch使用DataLoader實(shí)現(xiàn)批量加載數(shù)據(jù)

    Pytorch使用DataLoader實(shí)現(xiàn)批量加載數(shù)據(jù)

    這篇文章主要介紹了Pytorch使用DataLoader實(shí)現(xiàn)批量加載數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法

    Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇Python3遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)

    老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)

    下面小編就為大家?guī)硪黄仙U刾ython函數(shù)參數(shù)的區(qū)別(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 使用Selenium破解新浪微博的四宮格驗(yàn)證碼

    使用Selenium破解新浪微博的四宮格驗(yàn)證碼

    今天小編就為大家分享一篇關(guān)于使用Selenium破解新浪微博的四宮格驗(yàn)證碼的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖

    Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖

    與線型圖類似的是,散點(diǎn)圖也是一個個點(diǎn)集構(gòu)成的,下面這篇文章主要給大家介紹了關(guān)于Python中Matplotlib的點(diǎn)、線形狀、顏色以及繪制散點(diǎn)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 在django中,關(guān)于session的通用設(shè)置方法

    在django中,關(guān)于session的通用設(shè)置方法

    今天小編就為大家分享一篇在django中,關(guān)于session的通用設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論

霍林郭勒市| 九台市| 金堂县| 云阳县| 河池市| 郓城县| 哈尔滨市| 南和县| 嵩明县| 长寿区| 台东县| 仙桃市| 南昌县| 武隆县| 志丹县| 都江堰市| 云和县| 屏山县| 治多县| 杭锦后旗| 清镇市| 沅陵县| 靖西县| 当雄县| 泽库县| 紫金县| 茌平县| 钟山县| 从化市| 改则县| 云南省| 海阳市| 富蕴县| 桂平市| 襄汾县| 西乡县| 罗山县| 安平县| 北碚区| 徐水县| 谷城县|