博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 教程 第二十二章、 其它应用
阅读量:6413 次
发布时间:2019-06-23

本文共 4578 字,大约阅读时间需要 15 分钟。

第二十二章、 其它应用

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)
下载

#!/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()

转载于:https://www.cnblogs.com/lanzhi/p/6468775.html

你可能感兴趣的文章
tcpdump+wireshark 调试网络应用
查看>>
CentOS6.4配置yum源
查看>>
MFC 用CreateWindow创建的动态Button,如何指定ID?
查看>>
linux sed常用
查看>>
excel数据统计分析面面观
查看>>
jquery Map
查看>>
Linux下安装JDK的几种方法
查看>>
Centos6.5 WIFI无线网卡驱动BCM43142驱动安装
查看>>
linux 安装 oracle 11g
查看>>
我的友情链接
查看>>
windows注册表修改同步时间频率
查看>>
nginx的七层负载均衡
查看>>
TestNG report in IDEA
查看>>
mysql事务
查看>>
Oracle Virtualbox视图切换
查看>>
安装vCenter6.0遇到的问题以及解决方法
查看>>
zabbix调优
查看>>
我的友情链接
查看>>
Godaddy域名如何使用DNSPod做DNS解析
查看>>
linux系统故障
查看>>