第一次接触 com 编程,还挺好玩的,以前想要生成一份 ms office 文档的话,思路都是使用一些类库来直接生成,或者读写,原来还可以这样通过一个 com 像指挥一样向程序发指令让程序本身去执行,虽然没有写过 office 里面的 vbscript ,不过估计应该是跟这个挺类似的吧,有点意思。
想要使用 Python 进行 com 操作的话需要先安装 pywin32 ,在这里,http://sourceforge.net/project… ,一路 next 就行了。
先来看操作 excel 的:
#!/usr/bin/env python from Tkinter import Tk from time import sleep from tkMessageBox import showwarning import win32com.client as win32 warn = lambda app: showwarning(app, 'Exit?') RANGE = range(3, 8) def excel(): app = 'Excel' xl = win32.gencache.EnsureDispatch('%s.Application' % app) ss = xl.Workbooks.Add() sh = ss.ActiveSheet xl.Visible = True sleep(1) sh.Cells(1, 1).Value = 'Python-to-%s Demo' % app sleep(1) for i in RANGE: sh.Cells(i, 1).Value = 'Line %d' % i sleep(1) sh.Cells(i+2, 1).Value = "Th-th-th-that's all folks!" warn(app) ss.Close(False) xl.Application.Quit() if __name__ == '__main__': Tk().withdraw() excel()
代码应该比较简答,就不需要多解释了。
效果如图:
然后是操作 word 的:
#!/usr/bin/env python from Tkinter import Tk from time import sleep from tkMessageBox import showwarning import win32com.client as win32 warn = lambda app: showwarning(app, 'Exit?') RANGE = range(3, 8) def word(): app = 'Word' word = win32.gencache.EnsureDispatch('%s.Application' % app) doc = word.Documents.Add() word.Visible = True sleep(1) rng = doc.Range(0, 0) rng.InsertAfter('Python-to-%s Testrnrn' % app) sleep(1) for i in RANGE: rng.InsertAfter('Line %drnrn' % i) sleep(1) rng.InsertAfter("rnrnTh-th-th-th-that's all folks!rn") warn(app) doc.Close(False) word.Application.Quit() if __name__ == '__main__': Tk().withdraw() word()
效果如图:
然后是操作 powerpoint 的:
#!/usr/bin/env python from Tkinter import Tk from time import sleep from tkMessageBox import showwarning import win32com.client as win32 warn = lambda app: showwarning(app, 'Exit?') RANGE = range(3, 8) def ppoint(): app = 'PowerPoint' ppoint = win32.gencache.EnsureDispatch('%s.Application' % app) pres = ppoint.Presentations.Add() ppoint.Visible = True s1 = pres.Slides.Add(1, win32.constants.ppLayoutText) sleep(1) s1a = s1.Shapes[0].TextFrame.TextRange s1a.Text = 'Python-to-%s Demo' % app sleep(1) s1b = s1.Shapes[1].TextFrame.TextRange for i in RANGE: s1b.InsertAfter("Line %drn" % i) sleep(1) s1b.InsertAfter("rnTh-th-th-that's all folks!") warn(app) pres.Close() ppoint.Quit() if __name__ == '__main__': Tk().withdraw() ppoint()
效果如图:
最后一个是综合应用,从 yahoo 官网获得当前股票价格,然后打在 excel 里面:
#!/usr/bin/env python from Tkinter import Tk from time import sleep, ctime from tkMessageBox import showwarning from urllib import urlopen import win32com.client as win32 warn = lambda app: showwarning(app, 'Exit?') RANGE = range(3, 8) TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN') COLS = ('TICKET', 'PRICE', 'CHG', '%AGE') URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' def excel(): app = 'Excel' xl = win32.gencache.EnsureDispatch('%s.Application' % app) ss = xl.Workbooks.Add() sh = ss.ActiveSheet xl.Visible = True sleep(1) sh.Cells(1, 1).Value = 'Python-to-%s Stock Quote Demp' % app sleep(1) sh.Cells(3, 1).Value = 'Prices quoted as of: %s' % ctime() sleep(1) for i in range(4): sh.Cells(5, i+1).Value = COLS[i] sleep(1) sh.Range(sh.Cells(5, 1), sh.Cells(5, 4)).Font.Bold = True sleep(1) row = 6 u = urlopen(URL % ','.join(TICKS)) for data in u: tick, price, chg, per = data.split(',') sh.Cells(row, 1).Value = eval(tick) sh.Cells(row, 2).Value = ('%.2f' % round(float(price), 2)) sh.Cells(row, 3).Value = chg sh.Cells(row, 4).Value = eval(per.rstrip()) row += 1 sleep(1) u.close() warn(app) ss.Close(False) xl.Application.Quit() if __name__ == '__main__': Tk().withdraw() excel()
这个代码有的一说,首先是 30 行,原来选中多个单元格还可以这么 range 的,在其他语言里基本上没有想象过,然后 34 行,用一个逗号去 join 一个列表,这个也是很灵巧的写法,python 果然是个灵活的语言啊
效果如图: