今天一同事讓處理一個excel,把一個excel裡固定位置的內容讀取寫到另一個excel中的固定位置裡,查了一些資料,python有好多處理excel的模組,對比之後感覺用win32com來處理這個問題比較簡單,其中包含了處理檔案路徑和檔案名稱字為中文的編碼問題,下面是自己寫的代碼:
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
-
- from win32com.client import constants,Dispatch
- import os,sys,datetime,time,shutil
-
- class rw_excel:
-
- def __init__(self):
- self.yesterday = (datetime.date.today() - datetime.timedelta(days=2)).strftime('%Y%m%d')
- self.cwd = os.getcwd()
-
- def read_excel(self):
- try:
- f = self.cwd + "\\" + self.yesterday + "\\" + u"匯總".encode("gbk") + "_" + self.yesterday + ".xlsx"
- print f
- f_open = (f)
- xlsApp = Dispatch("Excel.Application")
- xlsApp.Visible = False
- xlsBook = xlsApp.Workbooks.Open(f_open)
- sheet_name = ('統計').decode("utf8")
- xlsSht = xlsBook.Worksheets(sheet_name)
- R = []
- 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]]
- for i in open:
- a = xlsSht.Cells(i[0],i[1]).Value.encode("utf8")
- R.append(a)
- return R
- except Exception,e:
- print e
-
- def write_excel(self,R):
- f = self.cwd + "\\" + self.yesterday + "\\" + u"數量匯總".encode("gbk") + "(GY)_" + self.yesterday + ".xls"
- print f
- f_save = (f)
- xlsApp = Dispatch("Excel.Application")
- xlsApp.Visible = False
- xlsBook = xlsApp.Workbooks.Open(f_save)
- sheet_name = ('數量匯總(GY)').decode("utf8")
- xlsSht = xlsBook.Worksheets(sheet_name)
- 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]]
- for i in range(len(R)):
- xlsSht.Cells(save[i][0],save[i][1]).Value = R[i]
- xlsBook.Close(SaveChanges=1)
- xlsApp.Quit()
-
- def main(self):
- R = self.read_excel()
- print "read_excel OK"
- self.write_excel(R)
- print "write_excel GY OK"
- print "Excel OK"
-
- class move:
-
- def __init__(self):
- self.yesterday = (datetime.date.today() - datetime.timedelta(days=2)).strftime('%Y%m%d')
- self.cwd = os.getcwd()
-
- def copy(self,src, dst):
- if os.path.isdir(src):
- base = os.path.basename(src)
- if os.path.exists(dst):
- dst = os.path.join(dst, base)
- if not os.path.exists(dst):
- os.makedirs(dst)
- names = os.listdir(src)
- for name in names:
- srcname = os.path.join(src, name)
- self.copy(srcname, dst)
- else:
- shutil.copy2(src, dst)
-
- def mk_dir(self):
- a = self.cwd + "\\" + u"處理名單_%s".encode("gbk") % self.yesterday + "\\" + u"處理名單(GY)_%s".encode("gbk") % self.yesterday
- print a
- if not os.path.isdir(a):
- os.makedirs(a)
- b = self.cwd + "\\" + u"處理名單_%s".encode("gbk") % self.yesterday + "\\" + u"處理名單(CS)_%s".encode("gbk") % self.yesterday
- print b
- if not os.path.isdir(b):
- os.makedirs(b)
- f = self.cwd + "\\" + self.yesterday + "\\"
- names = os.listdir(f)
- for name in names:
- if "txt" in name or "CS" in name:
- self.copy(os.path.join(f,name),os.path.join(b,name))
- else:
- self.copy(os.path.join(f,name),os.path.join(a,name))
- shutil.rmtree(f)
- print "Move ok"
-
- def main(self):
- self.mk_dir()
-
- if __name__=="__main__":
- boss = rw_excel()
- boss.main()
- boss = move()
- boss.main()
以下是運行列印的結果:
650) this.width=650;" border="0" alt="" src="http://www.bkjia.com/uploads/allimg/131228/125042J95-0.jpg" />
本文出自 “王偉” 部落格,請務必保留此出處http://wangwei007.blog.51cto.com/68019/983488