python核心编程学习笔记-2016-10-01-01-客户端COM编程

来源:互联网 发布:linux piwik 安装 编辑:程序博客网 时间:2024/05/18 15:07

        23.1

#-*-coding: utf-8-*-from time import ctimefrom urllib import urlopenticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN')URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2' # f=sl1c1p2中的s、l1、c1和p2分别代表股票订单号、上次交易价格、变化量和变化率print '\nPrice quoted as of:', ctime()print '\nTICKER'.ljust(9), 'PRICE'.ljust(8), 'GHG'.ljust(5), '%AGE'print '------'.ljust(8), '-----'.ljust(8), '---'.ljust(5), '----'u = urlopen(URL % ','.join(ticks))for row in u:    tick, price, chg, per = row.split(',')    print eval(tick).ljust(7), ('%.2f' % round(float(price), 2)).rjust(6), chg.rjust(6), eval(per.rstrip()).rjust(6)u.close()


         23.2 

         客户端COM编程:启动一个程序,并允许代码访问该应用程序提供的方法,被称为客户端的COM编程。

         服务端COM编程:实现一个COM对象供其他客户端调用则被称为服务端的COM编程。

         COM是组件对象模型。

         一般步骤:

  1. 启动应用程序;
  2. 打开要编辑的文档;
  3. 显示应用程序(如果有必要的话);
  4. 对文档做一定的操作;
  5. 保存或放弃文档;
  6. 推出    
         excel.py

#-*-coding: utf-8-*-from Tkinter import Tkfrom time import sleepfrom tkMessageBox import showwarning # 导入Tkinter和tkMessageBox是为了使用showwarning消息框来终止演示import win32com.client as win32warn = lambda app: showwarning(app, 'Exit?')RANGE = range(3, 8)def excel():    app = 'Excel'    x1 = win32.gencache.EnsureDispatch('%s.Application' % app) # 指定应用程序?第一步    ss = x1.Workbooks.Add() # 添加工作簿?    sh = ss.ActiveSheet # 正在显示的活动表格的句柄?第二步    x1.Visible = True # Visble必须标记为True,这样才能让应用程序显示在桌面上。就是第三步    sleep(1)    # 本程序的应用部分,第四步    sh.Cells(1,1).Value = 'Python-to-%s Demo' % app # 演示的标题,被写在左上角的第一格,即A1。    sleep(1)    for i in RANGE:        sh.Cells(i,1).Value = 'Line %d' % i # 在i1写入Line i        sleep(1)    sh.Cells(i+2,1).Value = "Th-th-th-that's all folks!" # 在A9显示Th-th-th-that's all folks!    warn(app) # 弹出showwarning对话框,显示内容见warn匿名函数的定义    ss.Close(False) # 表示电子表格关闭时不会被保存,但是本人在演示时仍然弹出是否保存的对话框。第五步    x1.Application.Quit() # 退出应用,第六步if __name__ == "__main__":    Tk().withdraw() # 隐藏顶层窗口    excel()

        word.py

#-*-coding: utf-8-*-# Word的例子与Excel的例子,但是前两次运行出错,第三次运行正常,不知道这是怎么回事。from Tkinter import Tkfrom time import sleepfrom tkMessageBox import showwarningimport win32com.client as win32warn = 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 Test\r\n\r\n' % app) # 与Excel不同的是,要写明行结束符,即回车换行    sleep(1)    for i in RANGE:        rng.InsertAfter('Line %d\r\n' % i)        sleep(1)    rng.InsertAfter("\r\nTh-th-th-that's all folks!\r\n")        warn(app)    doc.Close(False)    word.Application.Quit()if __name__ == "__main__":    Tk().withdraw()    word()

       ppoint.py

#-*-coding: utf-8-*-# 本演示报错,不解为何from Tkinter import Tkfrom time import sleepfrom tkMessageBox import showwarningimport win32com.client as win32warn = 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)    sla = s1.Shapes[0].TextFrame.TextRange    sla.Text = 'Python-to-%s Demo' % app    sleep(1)    slb = s1.Shapes[1].TextFrame.TextRange    for i in RANGE:        slb.InsertAfter("Line %d\r\n" % i)        sleep(1)    slb.InsertAfter("\r\nTh-th-th-that's all folks!")    warn(app)    pres.Close()    ppoint.Quit()if __name__ == "__main__":    Tk().withdraw()    ppoint()

      olook.py

#-*-coding: utf-8-*-from Tkinter import Tkfrom time import sleepfrom tkMessageBox import showwarningimport win32com.client as win32warn = lambda app: showwarning(app, 'Exit?')RANGE = range(3, 8)def outlook():    app = 'Outlook'    olook = win32.gencache.EnsureDispatch('%s.Application' % app)        mail = olook.CreateItem(win32.constants.olMailItem)    recip = mail.Recipients.Add('you@127.0.0.1')    subj = mail.Subject = 'Python-to-%s Demo' % app    body = ["Line %d" % i for i in RANGE]    body.insert(0, '%s\r\n' % subj)    body.append("\r\nTh-th-th-that's all folks!")    mail.Body = '\r\n'.join(body)    mail.Send()    ns = olook    obox = ns.GetDefaultFolder(win32.constants.olFolderOutbox)    obox.Display()    obox.Items.Item(1).Display()    warn(app)    olook.Quit()if __name__ == "__main__":    Tk().withdraw()    outlook()

        estock.py

#-*-coding: utf-8-*-# 就是之前的stock.py和excel.py结合起来from Tkinter import Tkfrom time import sleep, ctimefrom tkMessageBox import showwarningfrom urllib import urlopenimport win32com.client as win32warn = lambda app: showwarning(app, 'Exit?')RANGE = range(3, 8)TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN')COLS = ('TICKS', 'PRICE', 'CHG', '%AGE')URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'def excel():    app = 'Excel'    x1 = win32.gencache.EnsureDispatch('%s.Application' % app)    ss = x1.Workbooks.Add()    sh = ss.ActiveSheet    x1.Visible = True    sleep(1)    sh.Cells(1, 1).Value = 'Python-to-%s Stock Quote Demo' % 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)    x1.Application.Quit()if __name__ == "__main__":    Tk().withdraw()    excel()


1 0
原创粉丝点击