python 生成 ppt word outmail excel

# coding:utf-8
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning
import win32com.client as win32
from time import sleep, ctime
from urllib import urlopen

warn = lambda app: showwarning(app, 'Exit?')
RANGE = range(3, 10)

def excel():
    s='''I wasn't really shot with a silver bullet.'''
    sss = s.split()
    app = 'Excel'
    xl = win32.gencache.EnsureDispatch('%s.Application' % app)
    ss = xl.Workbooks.Add()
    sh = ss.ActiveSheet
    xl.Visible = True
    sleep(0.5)

    sh.Cells(1, 1).Value = 'Python-to-%s Demo' % app
    sleep(0.5)
    ii = 2
    for i in sss:
        print i
        sh.Cells(ii, 1).Value = i
        ii += 1
        sleep(0.5)

    sh.Cells(ii+2, 1).Value = "xixihahaha"
    warn(app)
    ss.Close(False)
    xl.Application.Quit()

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(3, 8):
        rng.InsertAfter('Line %d\r\n' % i)
        sleep(1)
    rng.InsertAfter('\r\nTh-th-th-thats all folks!')

    warn(app)
    doc.Close(False)
    word.Application.Quit()




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 Test\r\n\r\n' % app
    sleep(1)
    s1b = s1.Shapes[1].TextFrame.TextRange
    for i in range(3, 8):
        s1b.InsertAfter('Line %d\r\n' % i)
        sleep(1)
    s1b.InsertAfter("\r\nTh-th-th-thats all folks!")

    warn(app)
    pres.Close(False)
    ppoint.Quit()

def outlook():
    app = 'Outlook'
    olook = win32.gencache.EnsureDispatch('%s.Application' % app)
    mail = olook.CreateItem(win32.constants.olMailItem)
    recip = mail.Recipients.Add('815467444@qq.com')
    subj = mail.Subject = 'Python windows program demo  with outlook\n'
    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()

TICKS = ('BABA', 'GOOG', 'EBAY', 'AMZN')
COLS = ('TICKER', 'PRICE', 'CHG', '%AGE')
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'

def excell():
    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()
    excell()
    #excel()
    #word()
    #ppoint()
    # for i in range(20):
    #     outlook()
    #     sleep(5)
    #     print i





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值