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'\n代码'.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\r\n\r\n' % app) 
    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()
#!/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\r\n" % i) 
        sleep(1) 
    s1b.InsertAfter("\r\nTh-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\r\n' % subj) 
    body.append("\r\nTh-th-th-that's all folks!") 
    mail.Body = '\r\n'.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() 
posted @ 2011-10-13 12:28  方倍工作室  阅读(964)  评论(0编辑  收藏  举报