python用win32com處理excel表格

 今天一同事讓處理一個excel,把一個excel裏固定位置的內容讀取寫到另一個excel中的固定位置裏,查了一些資料,python有好多處理excel的模塊,對比之後感覺用win32com來處理這個問題比較簡單,其中包含了處理文件路徑和文件名字爲中文的編碼問題,下面是自己寫的代碼:

  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  4. from win32com.client import constants,Dispatch 
  5. import os,sys,datetime,time,shutil 
  6.  
  7. class rw_excel: 
  8.  
  9.     def __init__(self): 
  10.         self.yesterday = (datetime.date.today() - datetime.timedelta(days=2)).strftime('%Y%m%d'
  11.         self.cwd = os.getcwd() 
  12.  
  13.     def read_excel(self): 
  14.         try
  15.             f = self.cwd + "\\" + self.yesterday + "\\" + u"彙總".encode("gbk") + "_" + self.yesterday + ".xlsx" 
  16.             print f 
  17.             f_open = (f) 
  18.             xlsApp = Dispatch("Excel.Application"
  19.             xlsApp.Visible = False 
  20.             xlsBook = xlsApp.Workbooks.Open(f_open) 
  21.             sheet_name = ('統計').decode("utf8"
  22.             xlsSht = xlsBook.Worksheets(sheet_name) 
  23.             R = [] 
  24.             open = [[2,2],[3,2],[4,2],[5,2],[6,2],[7,2],[8,2],[9,2],[10,2],[11,2],[12,2],[13,2],[14,2],[15,2]] 
  25.             for i in open: 
  26.                 a = xlsSht.Cells(i[0],i[1]).Value.encode("utf8"
  27.                 R.append(a) 
  28.             return R 
  29.         except Exception,e: 
  30.             print e 
  31.  
  32.     def write_excel(self,R): 
  33.         f = self.cwd + "\\" + self.yesterday + "\\" + u"數量彙總".encode("gbk") + "(GY)_" + self.yesterday + ".xls" 
  34.         print f 
  35.         f_save = (f) 
  36.         xlsApp = Dispatch("Excel.Application")    
  37.         xlsApp.Visible = False 
  38.         xlsBook = xlsApp.Workbooks.Open(f_save) 
  39.         sheet_name = ('數量彙總(GY)').decode("utf8"
  40.         xlsSht = xlsBook.Worksheets(sheet_name) 
  41.         save = [[2,5],[3,5],[6,5],[7,5],[9,5],[10,5],[12,5],[13,5],[14,5],[17,5],[18,5],[19,5],[20,5],[23,5]] 
  42.         for i in range(len(R)): 
  43.             xlsSht.Cells(save[i][0],save[i][1]).Value = R[i] 
  44.         xlsBook.Close(SaveChanges=1
  45.         xlsApp.Quit() 
  46.  
  47.     def main(self): 
  48.         R = self.read_excel() 
  49.         print "read_excel OK" 
  50.         self.write_excel(R) 
  51.         print "write_excel GY OK" 
  52.         print "Excel OK" 
  53.  
  54. class move: 
  55.  
  56.     def __init__(self): 
  57.         self.yesterday = (datetime.date.today() - datetime.timedelta(days=2)).strftime('%Y%m%d'
  58.         self.cwd = os.getcwd() 
  59.  
  60.     def copy(self,src, dst): 
  61.         if os.path.isdir(src): 
  62.             base = os.path.basename(src) 
  63.             if os.path.exists(dst): 
  64.                 dst = os.path.join(dst, base) 
  65.             if not os.path.exists(dst): 
  66.                 os.makedirs(dst) 
  67.             names = os.listdir(src) 
  68.             for name in names: 
  69.                 srcname = os.path.join(src, name) 
  70.                 self.copy(srcname, dst) 
  71.         else
  72.             shutil.copy2(src, dst) 
  73.  
  74.     def mk_dir(self): 
  75.         a = self.cwd + "\\" + u"處理名單_%s".encode("gbk") % self.yesterday + "\\" + u"處理名單(GY)_%s".encode("gbk") % self.yesterday 
  76.         print a 
  77.         if not os.path.isdir(a): 
  78.             os.makedirs(a) 
  79.         b = self.cwd + "\\" + u"處理名單_%s".encode("gbk") % self.yesterday + "\\" + u"處理名單(CS)_%s".encode("gbk") % self.yesterday 
  80.         print b 
  81.         if not os.path.isdir(b): 
  82.             os.makedirs(b) 
  83.         f = self.cwd + "\\" + self.yesterday + "\\" 
  84.         names = os.listdir(f) 
  85.         for name in names: 
  86.             if "txt" in name or "CS" in name: 
  87.                 self.copy(os.path.join(f,name),os.path.join(b,name)) 
  88.             else
  89.                 self.copy(os.path.join(f,name),os.path.join(a,name)) 
  90.         shutil.rmtree(f) 
  91.         print "Move ok" 
  92.  
  93.     def main(self): 
  94.         self.mk_dir() 
  95.  
  96. if __name__=="__main__"
  97.     boss = rw_excel() 
  98.     boss.main() 
  99.     boss = move() 
  100.     boss.main() 

以下是運行打印的結果:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章