zoukankan      html  css  js  c++  java
  • python 教程 第二十二章、 其它应用

    第二十二章、 其它应用
    1)    Web服务

    ##代码  s     000063.SZ 
    ##开盘  o     26.60 
    ##最高  h     27.05 
    ##最低  g     26.52 
    ##最新  l1    26.66 
    ##涨跌  c1    -0.04 
    ##涨幅  p2    -0.15% 
    ##总手  v     9190865 
    ##日期  d1    6/15/2011 
    ##时间  t1    3:00am  
    
    
    #!/usr/bin/env python  
    
    
    from time import ctime 
    from urllib import urlopen 
    import re  
    
    
    ticks = ('000063.sz', '600001.ss', '010110.ss', '000005.sz', 
             '300003.SZ', '000100.sz', '600519.ss', '900950.SS') 
    URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sohgl1v' 
    
    
    print 'Prices quoted as of:', ctime() 
    print  u'
    代码'.rjust(1), u'开盘'.rjust(6), u'最高'.rjust(4),  
             u'最低'.rjust(4), u'现价'.rjust(4), u'总手'.rjust(7) 
    u = urlopen(URL % ','.join(ticks))  
    
    
    for row in u: 
        name, openp, high, low, close, volume = row.split(',') 
        print  (re.search('[0-9]{6}', name).group()).rjust(6),  
               (re.search('d+.d{2}|N/A', openp).group()).rjust(6),  
               (re.search('d+.d{2}|N/A', high).group()).rjust(6),  
               (re.search('d+.d{2}|N/A', low).group()).rjust(6),  
               (re.search('d+.d{2}|N/A', close).group()).rjust(6),  
               (re.search('[0-9]+|N/A', volume).group()).rjust(9) 
    u.close()  

    2)    COM编程
    安装pywin32(pywin32-216.win32-py2.7.exe)
    下载http://sourceforge.net/projects/pywin32/files/pywin32/

    #!/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 Test
    
    ' % app) 
        sleep(1) 
        for i in RANGE: 
            rng.InsertAfter('Line %d
    ' % i) 
            sleep(1) 
        rng.InsertAfter("
    Th-th-th-that's all folks!
    ")  
    
    
        warn(app) 
        doc.Close(False) 
        word.Application.Quit()  
    
    
    if __name__=='__main__': 
        Tk().withdraw() 
        word()
    #!/usr/bin/env python (estock.pyw)  
    
    
    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 = ('TICKER', '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 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) 
        xl.Application.Quit()  
    
    
    if __name__=='__main__': 
        Tk().withdraw() 
        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 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 %d
    " % i) 
            sleep(1) 
        s1b.InsertAfter("
    Th-th-th-that's all folks!") 
    
    
        warn(app) 
        pres.Close() 
        ppoint.Quit() 
    
    
    if __name__=='__main__': 
        Tk().withdraw() 
        ppoint() 
    #!/usr/bin/env python 
    
    
    from Tkinter import Tk 
    #from time import sleep    # SUPERFLUOUS 
    from tkMessageBox import showwarning 
    import win32com.client as win32 
    
    
    warn = 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('hello@msn.com') 
        subj = mail.Subject = 'Python-to-%s Demo' % app 
        body = ["Line %d" % i for i in RANGE] 
        body.insert(0, '%s
    ' % subj) 
        body.append("
    Th-th-th-that's all folks!") 
        mail.Body = '
    '.join(body) 
        mail.Send() 
    
    
        ns = olook.GetNamespace("MAPI") 
        obox = ns.GetDefaultFolder(win32.constants.olFolderOutbox) 
        obox.Display() 
        obox.Items.Item(1).Display() 
    
    
        warn(app) 
        olook.Quit() 
    olook = outlook 
    
    
    if __name__=='__main__': 
        Tk().withdraw() 
        outlook() 
  • 相关阅读:
    Kubernetes 集成研发笔记
    Rust 1.44.0 发布
    Rust 1.43.0 发布
    PAT 甲级 1108 Finding Average (20分)
    PAT 甲级 1107 Social Clusters (30分)(并查集)
    PAT 甲级 1106 Lowest Price in Supply Chain (25分) (bfs)
    PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)
    PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)
    java 多线程 26 : 线程池
    OpenCV_Python —— (4)形态学操作
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468775.html
Copyright © 2011-2022 走看看